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

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么