anulik anulik - 1 month ago 8
PHP Question

.htaccess is not redirecting from old url to new url 3

I am new in

.htaccess
. I have read the documentation, but couldn't make the redirect rule work.

I want
RewriteRule
for following

http://mywebsite.com/restaurants/59393/the-grounds-of-alexandria.htm


to

http://mywebsite.com/restaurants/the-grounds-of-alexandria


I have tried out this

RewriteRule ^(.*)/[0-9]/(.*)$ ^(.*)/(.*)/$ [R=301,L]


Also how can u get the subdomain and put it to the front of all links, like this

http://sydney.mywebsite.com/restaurants/59393/the-grounds-of-alexandria.htm


to

http://mywebsite.com/sydney/restaurants/the-grounds-of-alexandria


but no result.

Answer

Rewrite this is simple:

http://mywebsite.com/restaurants/59393/the-grounds-of-alexandria.htm
to
http://mywebsite.com/restaurants/the-grounds-of-alexandria

Your rewrite rule RewriteRule ^(.*)/[0-9]/(.*)$ ^(.*)/(.*)/$ [R=301,L] is too global for the urls. Second of all you just say [0-9] with means only one digit.

RewriteRule ^([^/]+)/[0-9]+/([^.]+)\.htm$ /$1/$2 [R=301,L]

Try this one ;)


Edit (for second question):

http://sydney.mywebsite.com/restaurants/59393/the-grounds-of-alexandria.htm
to 
http://mywebsite.com/sydney/restaurants/the-grounds-of-alexandria

Sure ;) There you have to work with RewriteCond.

RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]+)\.mywebsite\.com/([^/]+)/[0-9]+/([^.]+)\.htm
RewriteRule ^.*$ http://mywebsite.com/%1/%2/%3 [R=301,L]

For explanation, the %1-3 can be used from the previous match to be used in the RewriteRule Statement. This should work for every subdomain and only for subdomains. If you want a specific subdomain you can adjust the first line to ^(sidney|othersubdomain|anothersubdomain)\.mywebsite\.com....