Reputation: 1347
I'm trying to redirect a dynamic page to a dir, example:
url.com/index.php?page=download
to
url.com/download
The rule is very simple:
RewriteRule ^download$ /index.php?page=download
FULL .HTACCESS
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.url\.com$
RewriteRule ^(.*) http://url.com/$1 [R=301,L]
Redirect 301 /index.php?page=download http://url.com/download
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteRule ^download$ /index.php?page=download
The problem is that I already have a directory named "download" and it's returning me a "403 Forbidden Error". I have noticed that renaming the directory to anything different than the name (download) the rule is using will work.
So, my question is: how can I have my url rewritten the way I need and keep the directory?
-- EDIT --
My server, by default, protects every directory with a "403 Forbbiden Error". I believe this is the reason why it's not working, but I'm not sure. Is it a conflict indeed? What is happening?
Upvotes: 1
Views: 1336
Reputation: 1347
/download/?page=download
The reason why this is happening is because somewhere mod_dir redirects all requests for directories that are missing the trailing slash to include the trailing slash. This is interferring with your rewrite rule. Since your server is automatically setup to deny listing of directories, it's probably safe to go ahead and turn directory slashes off:
DirectorySlash Off
Answer by: https://stackoverflow.com/users/851273/jon-lin
Upvotes: 1
Reputation: 19672
Your problem is, I think, due to this specific rewrite rule:
RewriteRule .* - [L]
Apache will read your htaccess in detail, line by line, and will try to map each rewrite rule in turn (+ conditions, obviously). Let's follow it in detail.
RewriteCond %{HTTP_HOST} !^www\.url\.com$
RewriteRule ^(.*) http://url.com/$1 [R=301,L]
These two by themselves are NOT fine but not the source of your problem. If the HTTP HOST is anything but www.url.com, it will redirect to http://url.com/... which is not www.url.com, which will then redirect to url.com. You have a potential infinite loop but only if the HTTP HOST is not www.url.com to start with. If it matched, due to the L flag, it breaks processing and returns the redirect.
Redirect 301 /index.php?page=download http://url.com/download
This line is fine. You're doing a 301 redirect to anyone who happens to know index.php?page=download.
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
This line is NOT fine. The rewrite condition specifies that, so far, anything that is not a redirect from above will work for the next rule. The next rule matches EVERYTHING (.* means everything. 0 or more characters of anything) and in-rewrites to -, which in mod_rewrite language is a do not touch flag. This is fine. However, the L flag breaks the chain and returns, which causes your download/ folder to show as the list of your actual download folder, which is the source of your 403 error.
Remove the L flag to get the next rule to process:
RewriteRule ^download$ /index.php?page=download
Which is fine by itself.
Upvotes: 0