这个mod_rewrite规则让我循环

时间:2014-09-02 18:35:27

标签: apache .htaccess mod-rewrite

我希望/path/short提供文件/path/short.html所拥有的内容,但如果用户输入/path/short.html/path/short/,我希望将其重定向(使用301)到/path/short。这是我目前在.htaccess

中的内容
RewriteEngine On
RewriteBase /path
RewriteCond %{REQUEST_URI} !=/path/handle.html
RewriteCond %{REQUEST_URI} !^/path/short(.html|/)?$
RewriteRule ^(.+) /cgi-bin/handle\.cgi?$1 [PT,QSA]

RewriteRule ^path$ /path/short.html

#-------
RewriteRule ^short/$ /path/short [R=301,L]
# RewriteRule ^short.html$ /path/short [R=301,L]

规则反映了handle.cgi将收到所有其他请求(即/path/this/path//path/somethingelse等)的事实,这些请求效果很好。到目前为止,/path/short/正确地重定向到/path/short/path/short正确显示/path/short.html中的内容,但是,取消注释最后一行会导致循环。那么,我该如何让它发挥作用呢?

2 个答案:

答案 0 :(得分:0)

您可以在/path/.htaccess

中使用此代码
RewriteEngine On
RewriteBase /path/

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

RewriteRule ^(short)/?$ $1.html [L,NC]

答案 1 :(得分:0)

一位朋友向我指出了另一个帮助我解决这个问题的问题:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/ads/short(.html|/)?$
RewriteRule ^(.+) /cgi-bin/handle\.cgi?$1 [PT,QSA] 

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^short(.html|/)$ /ads/short [R=301]
RewriteRule ^short$ /ads/short.html

完美无缺。