我.htaccess
的根目录中的文件下面有一个重写规则:
RewriteEngine On
# Exclude .css / .js / ...
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
# Rewrite Rule
RewriteRule ^([^/]*)/([^/]*)$ /index.php?category=$1&product=$2
因此,网址http://example.com/data1/data2
给了我category=data1
和product=data2
。
问题在于以下URL无法正常工作:
http://example.com/data1 # Not Working (Page Not Found)
http://example.com/data1/data2/ # Not Working (Page Not Found)
但是这些URL可以正常工作:
http://example.com/data1/ # Works -> category=data1
http://example.com/data1/data2 # Works -> category=data1 & product=data2
如何将前两个URL重定向到第二个?
或/与
进行所有URL都重定向到非跟踪斜杠的操作。所以下面的URL:
http://example.com/data1/
http://example.com/data1/data2/
重定向到这些:
http://example.com/data1
http://example.com/data1/data2
为避免尾随斜杠一起使用,请执行重定向
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R,L]
要仅使用一个元素来匹配网址,您可以使用第二条规则
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?category=$1&product= [L]
或者
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?category=$1 [L]
放在一起给
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R,L]
RewriteRule ^(.+?)/(.+)$ /index.php?category=$1&product=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?category=$1 [L]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句