Forum Moderators: phranque
I got two quesions, couldn't find the answer anywhere.
I'm trying to rewrite URLs with following structure
eg. [domain.co.uk...]
to
[domain.co.uk...]
My .htaccess file:
AddType application/x-httpd-php .htm .html
rewriteEngine on
RewriteRule ^([^/]+)/([a-z]),([0-9]{1,6})\/ [domain.co.uk...] [L]
1. First question is Why do I need to include the full web address, when I use
RewriteRule ^([^/]+)/([a-z]),([0-9]{1,6})\/ index.html?action=$2&id=$3§ion=$1 [L]
I get a 404 saying:
The requested URL /home/luke/public_html/website/index.html was not found on this server.
2. Second question is why when use the full web address URL after the URL is rewritten and a page opens the URL in the address bar is:
[domain.co.uk...]
not
[domain.co.uk...]
Many thanks.
Regards,
Luke
mod_rewrite can perform several different functions:
A internal rewrite changes the filename used to provide content for a requested URL. An internal rewrite does not communicate with the client browser, it simply says, for example, "When the client requests the URL /foo.gif, serve the contents of the file /bar.gif instead."
An external redirect provides no content in response to a client request. Instead, it sends back a message to the client that says, "The content you requested has moved to a different URL. You may find it at this new URL." The server then provides the new URL given in the code that was used to invoke the redirect response. It is up to the client to re-request the content from the new URL. When a client browser does so, it changes the address in its address bar.
The third function is a proxy throughput. It sends the request to Apache's proxy module, which (usually) is used to forward the request to another server, so that content from that other server can be provided in response to the request.
With this in mind, the answer to your question is that a canonical URL must be provided in order to do either an external redirect or a proxy throughput. It should not be necessary to provide a canonical address for an internal rewrite. It is obvious that in your application an internal rewrite is what you want to do.
The problem you are seeing often appears when a server is configured to use a different filepath at the level above the URL-path following the domain name. This gets a little hard to describe, but if
URL: example.com/directory/filename
does not directly relate to
Filepath: <Document_Root>/direcotry/filename
Then you can see this problem. It usually appears when the server is configured using an Alias directive, or when the server makes use of mod_userdir.
mod_rewrite [httpd.apache.org] provides a solution with its RewriteBase [httpd.apache.org] directive.
For a first try, I would suggest:
AddType application/x-httpd-php .htm .html
RewriteEngine on
RewriteBase /home/luke
RewriteRule ^([^/]+)/([a-z]),([0-9]{1,6})/ /index.html?action=$2&id=$3§ion=$1 [L]
By examining the attempted filepath in your error log, you should be able to 'adjust' this if I didn't get it quite right.
[added] I would also suggest changing the second line from
AddType application/x-httpd-php .htm .html
to
AddHandler server-parsed .htm .html
[/added]
Jim