mod_rewrite code returns wrong domain, changed symbolic link to actual
I have two domains for the same website.
I have a single page on my site I want to appear to come from the second domain (in this example it is example2.com/pagex). With exception to root and pagex, all other requests to example2.com should be redirected to example1.com.
All pages on my site appear as directories but are really just index.php?page=pagename due to mod_rewrite rules shown below.
Here is my .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.com [NC]
RewriteCond %{REQUEST_URI} !^/$ #Do not redirect root
RewriteCond %{REQUEST_URI} !开发者_C百科^/pagex/ #Do not redirect pagex
RewriteRule ^(.*)$ http://www.example1.com/$1 [R=301]
# If the file or folder does not exist, send to index.php as variable
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ /index.php?page=$1 [L]
Running the above code by going to example2.com/pagex/ I get redirected to www.example1.com/index.php?page=pagex. The second condition should have failed, preventing the redirect. What is even stranger is the virtual directory has been replaced with the actual path.
You can write your rules like this:
RewriteEngine on
Options +FollowSymlinks -MultiViews
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.com [NC]
RewriteCond %{THE_REQUEST} !/pagex/? [NC]
RewriteRule ^(.+)$ http://www.example1.com/$1 [R=301,L]
# If the file or folder does not exist, send to index.php as variable
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ /index.php?page=$1 [L]
With above code http://example2.com/pagex/
gets correctly redirected to http://example2.com/index.php?page=pagex
精彩评论