Reputation: 148
I'm setting up a working copy of a site, and copied the entire site to a mamp. But I have problem setting up a working version of .htaccess.
Working directory on localhost is "folder1".
This is the live .htaccess:
RewriteEngine On
RewriteBase /
# Rewrite site site.eu to site.eu/en/
RewriteCond %{QUERY_STRING} !language=(sv|en|ru|jp)
RewriteRule ^$ en/ [L,R=301]
# Add trailing slash if necessary
RewriteRule ^(sv|en|ru|jp)$ $1/ [R=301,L]
# Add language part to old bookmarks/links
RewriteCond %{REQUEST_URI} !^/(sv|en|ru|jp)/
RewriteCond %{QUERY_STRING} !language=(sv|en|ru|jp)
RewriteRule (sale|brands|latest_in_stock)(.*) en/$1$2 [R=301,L]
# Remove www
RewriteCond %{HTTP_HOST} ^www\.site\.eu$ [NC]
RewriteRule (.*) http://site.eu/$1 [R=301,L]
RewriteRule ^(sv|en|ru|jp)/(.*)$ $2?language=$1 [L,QSA]
RewriteRule ^sale sale.php [L,QSA,NC]
RewriteRule ^brands/([^/]+) /designer.php?idnr=$1 [L,QSA,NC]
RewriteRule ^brands /labelList.php [L,QSA,NC]
...
On the local copy I changed to:
RewriteBase /folder1/
I tried to removed the part with "Add language code to old... " and the part "Remove www". I also tried different changes to the last RewriteRule for exampel adding "folder1" and remove it from RewriteBase.
RewriteRule ^brands /folder1/labelList.php [L,QSA,NC]
The php-files and the index-file is working fine, but none of the /brands for example. I get a 500 error, too many redirects.
Upvotes: 1
Views: 134
Reputation: 20737
The variable %{REQUEST_URI}
will contain the part behind the domain name, regardless of which .htaccess file it is in. What will happen is the following:
We start with localhost/en/brands/asdf
. It matches the following rule:
RewriteCond %{REQUEST_URI} !^/(sv|en|ru|jp)/
RewriteCond %{QUERY_STRING} !language=(sv|en|ru|jp)
RewriteRule (sale|brands|latest_in_stock)(.*) en/$1$2 [R=301,L]
The first condition is true, because ^/(sv|en|ru|jp)/
does not match /folder1/en/...
. The second condition is true, because we haven't reached the rule yet that sets the language in the query string. Finally it will match the last one, because (sale|brands|latest_in_stock)(.*)
will match en/brands/asdf
($1
will contain brands
and $2
will contain /asdf
). This will redirect to localhost/folder1/en/brands/asdf
after re-adding the directory-prefix.
You have several options:
1. adding folder1 to the condition
If you add folder1 to the condition with %{REQUEST_URI}
, this problem does not occur:
RewriteCond %{REQUEST_URI} !^/folder1/(sv|en|ru|jp)/
2. adding a negative look-ahead
If you add a negative look-ahead to the rule, and remove the condition, things work out correctly, because the first argument of RewriteRule
will match against what you expect. The pro of this is that you do not need to know in which directory this is:
#Remove this: RewriteCond %{REQUEST_URI} !^/(sv|en|ru|jp)/
RewriteCond %{QUERY_STRING} !language=(sv|en|ru|jp)
RewriteRule ^(?!(sv|en|ru|jp)/)[^/]*/?(sale|brands|latest_in_stock)(.*) en/$1$2 [R=301,L]
You might need to alter the capture group references. I can't test them here.
3. Moving rules around
Moving the following rule:
RewriteRule ^(sv|en|ru|jp)/(.*)$ $2?language=$1 [L,QSA]
just above the rule I just pointed out should theoretically solve this problem. I would strongly recommend against this solution, because intermixing redirects and rewrites makes it very hard to read.
Upvotes: 1