URL rewrite for part of domain name

2019-09-05 22:58发布

问题:

I feel this should be an easy fix but I'm struggling to get it done right. I have the URL:

  • http://www.testing.com/toursgbr/my-post

  • http://www.testing.com/toursgbr/my-post-2

I need to rewrite the URL to:

  • http://www.testing.com/tours/gbr/my-post

  • http://www.testing.com/tours/gbr/my-post-2

I got as far as the following:

RewriteRule ^toursgbr/(.*)   /tours\/gbr/$1   [L]

This is what's currently in the htaccess file:

RewriteEngine on
ErrorDocument 404 http://www.ausweb.com.au/web-hosting
AddHandler server-parsed html

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
RewriteCond %{HTTP_HOST} ^seabreezepark\.com\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.seabreezepark\.com\.au$
RewriteRule ^/?$ "http\:\/\/theseabreezepark\.com\.au\/" [R=301,L]
RewriteRule ^toursgbr/(.+)$ /tours/gbr/$1 [NC,L]

and got nowhere pretty fast. I just want to look for the word "toursgbr" and change it to "tours/gbr" in summary.

回答1:

Put the Following code at root .htaccess file :

RewriteEngine on
RewriteBase /

RewriteCond %{THE_REQUEST} !\s/+tours/gbr/ [NC]

# the above line will exclude any request having tours/gbr/ from the follwoing rule.

RewriteRule ^toursgbr/(.*)$ tours/gbr/$1 [R=302,L,NE]

# the above line will change any requested url having toursgbr/ to be tours/gbr/ temporary
# and you can change it to permanent by changing [R=302,L,NE] to [R=301,L,NE]  
# but check the code as it is first then change it

RewriteRule ^tours/gbr/(.*)$ toursgbr/$1 [L,NC]

# the above line will internally map any request having tours/gbr/ to its original path


回答2:

Try :

RewriteRule ^toursgbr/(.+)$ /tours/gbr/$1 [NC,L]

Do not use the full url in rewrite target if you want to internally redirect /toursgbr/foo to /tours/gbr/foo without changing the url in browser.

Your corrected htaccess :

ErrorDocument 404 http://www.ausweb.com.au/web-hosting
AddHandler server-parsed html

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?seabreezepark\.com\.au$
RewriteRule ^/?$ http://theseabreezepark.com.au/ [L,R=301]
RewriteRule ^toursgbr/(.+)$ /tours/gbr/$1 [NC,L]
 # BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]