1

我正在尝试对域进行一些重写。htaccess 当前设置为考虑正斜杠后的某些内容。例如。mysite.dev/page1/

我希望将其更改为:mysite.dev/page1.html

所以我在原始链接上提出了一个转发,并为 .html 页面设置了新的重写,如下所示:

Redirect 301 /page1/ http://mysite.dev/page1.html
Redirect 301 /page1 http://mysite.dev/page1.html
Redirect 301 /page2/ http://mysite.dev/page2.html
Redirect 301 /page2 http://mysite.dev/page2.html
Redirect 301 /page3/ http://mysite.dev/page3.html
Redirect 301 /page3 http://mysite.dev/page3.html

RewriteRule ^page1.html$ page.php?slug=page1 [L]
RewriteRule ^page2.html$ page.php?slug=page2 [L]
RewriteRule ^page3.html$ page.php?slug=page3 [L]

RewriteRule ^([^/.]+)/?$ page.php?slug=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ page.php?slug=$2 [L]

当去 mysite.dev/page1.html 它工作正常。但是当放置 mysite.dev/page1/ 时,它会重写为 mysite.dev/page1.html?slug=page1。所以它似乎继续遵循其余的规则。

关于解决此问题的任何帮助/建议?

谢谢。

4

1 回答 1

1

避免混合 mod_alias 和 mod_rewrite 规则。

还要在第一个 RewriteRule 之前添加此规则以避免循环:

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

完成.htaccess:

RewriteEngine On

RewriteCond %{THE_REQUEST} /(page1|page2|page3)[/\s\?] [NC]
RewriteRule ^ /%1.html [L,R=301]

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^(page1|page2|page3)\.html$ page.php?slug=$1 [L,QSA,NC]

RewriteRule ^([^/.]+)/?$ page.php?slug=$1 [L,QSA]

RewriteRule ^([^/.]+)/([^/.]+)/?$ page.php?slug=$2 [L,QSA]
于 2013-11-12T09:28:36.433 回答