URL rewrite for part of domain name

半腔热情 提交于 2019-12-12 03:25:21

问题


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]


来源:https://stackoverflow.com/questions/37129363/url-rewrite-for-part-of-domain-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!