简体   繁体   English

.htaccess不会从旧网址重定向到新网址3

[英].htaccess is not redirecting from old url to new url 3

I am new in .htaccess . 我是.htaccess新手。 I have read the documentation, but couldn't make the redirect rule work. 我已经阅读了文档,但是无法使重定向规则起作用。

I want RewriteRule for following 我想要以下的RewriteRule

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. 但没有结果。

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. 您的重写规则RewriteRule ^(.*)/[0-9]/(.*)$ ^(.*)/(.*)/$ [R=301,L]对于网址而言过于全局。 Second of all you just say [0-9] with means only one digit. 其次,您只说[0-9]意味着只有一位数字。

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。

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. 为了便于说明,可以从RematchRule语句中使用的上一个匹配项中使用%1-3。 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... . 如果需要特定的子域,则可以将第一行调整为^(sidney|othersubdomain|anothersubdomain)\\.mywebsite\\.com...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM