三种独立的方案:
foo.html
,没有文件夹foo
:/foo.hmtl
foo
所在的文件夹index.html
:/foo/index.html
foo
所在的文件夹bar.html
:/foo/bar.html
在所有三种情况下的目标是:
该规范的网址应为example.org/foo
:
example.org/foo.html
应该重定向到 example.org/foo
example.org/foo/
应该重定向到 example.org/foo
example.org/foo//
应该重定向到 example.org/foo
example.org/foo/index.html
应该重定向到 example.org/foo
example.org/foo/bar.html
应该重定向到 example.org/foo
方案#1的第一种方法:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule foo foo.html [NC,L]
请求example.org/foo
返回的内容,/foo.html
但当然没有从example.org/foo.html
到的重定向example.org/foo
。
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule foo foo.html [NC,L]
Redirect 301 foo.html foo
请求example.org/foo
或example.org/foo.html
返回HTTP状态代码500 Internal Server Error。
方案#1的第二种方法:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule foo foo.html [R=301,NC,L]
请求example.org/foo
重定向到example.org/foo.html
,然后返回的内容/foo.html
。
...
我尝试了更多,但没有得到想要的结果。那么有人可以帮我,像我三岁时向我解释吗?;-)
您可以尝试以下吗?
RewriteEngine On
### from example.org/foo/ OR example.org/foo// ----> example.org/foo ####
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(foo)/+?$
RewriteRule ^.*$ /%1 [NC,R=302,L]
### redirect foo.html to foo with showing foo.html content ####
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/(foo)\.html$ [NC]
RewriteRule ^.*$ /%1 [NC,R=302,L]
###redirect from foo/index.html to foo AND foo/bar.html to foo ####
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/(foo)/(index|bar)\.html [NC]
RewriteRule ^.*$ /%1 [NC,R=302,L]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句