drgd73844 2014-11-07 12:38
浏览 21
已采纳

.htaccess没有从旧网址重定向到新网址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.

  • 写回答

1条回答 默认 最新

  • dongzong7467 2014-11-07 14:56
    关注

    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....

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?