首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.htaccess不会将旧url重定向到新url 3。

.htaccess不会将旧url重定向到新url 3。
EN

Stack Overflow用户
提问于 2014-11-07 12:38:10
回答 1查看 34关注 0票数 0

我是.htaccess的新手。我已经阅读了文档,但无法使重定向规则发挥作用。

我想要RewriteRule负责以下工作

代码语言:javascript
复制
http://mywebsite.com/restaurants/59393/the-grounds-of-alexandria.htm

代码语言:javascript
复制
http://mywebsite.com/restaurants/the-grounds-of-alexandria

我试过这个

代码语言:javascript
复制
RewriteRule ^(.*)/[0-9]/(.*)$ ^(.*)/(.*)/$ [R=301,L]

另外,您如何获得子域并将其放在所有链接的前面,就像这样

代码语言:javascript
复制
http://sydney.mywebsite.com/restaurants/59393/the-grounds-of-alexandria.htm

代码语言:javascript
复制
http://mywebsite.com/sydney/restaurants/the-grounds-of-alexandria

但没有结果。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-07 14:56:43

重写这很简单:

代码语言:javascript
复制
http://mywebsite.com/restaurants/59393/the-grounds-of-alexandria.htm
to
http://mywebsite.com/restaurants/the-grounds-of-alexandria

重写规则RewriteRule ^(.*)/[0-9]/(.*)$ ^(.*)/(.*)/$ [R=301,L]对于urls来说太全局了。其次,你只要说0-9就意味着只有一位数。

代码语言:javascript
复制
RewriteRule ^([^/]+)/[0-9]+/([^.]+)\.htm$ /$1/$2 [R=301,L]

(试试这个;)

编辑(关于第二个问题):

代码语言:javascript
复制
http://sydney.mywebsite.com/restaurants/59393/the-grounds-of-alexandria.htm
to 
http://mywebsite.com/sydney/restaurants/the-grounds-of-alexandria

当然;)在那里,您必须使用RewriteCond。

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

为了进行解释,%1-3可以从要在RewriteRule语句中使用的上一次匹配中使用。这应该适用于每个子域,而且只适用于子域。如果您想要一个特定的子域,可以将第一行调整为^(sidney|othersubdomain|anothersubdomain)\.mywebsite\.com...

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26801427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档