htaccess导致浏览器使用正斜杠重定向

时间:2017-07-27 02:20:16

标签: php .htaccess

我在设置网站的htaccess文件时遇到问题。问题是当我访问没有正斜杠的特定位置链接时:

example.com/location/city

然后重定向到:

example.com/location ///////

并且不断重写每次添加正斜杠。

仅当我从末尾删除正斜杠时才会发生这种情况。如果正斜杠在那里,页面加载正常。网站上的所有其他页面都运行良好。

这是我的代码:

RewriteEngine On
RewriteBase /

#####################################################
# Rewrite specific pages - listings
RewriteCond %{THE_REQUEST} \ /+listings\.php\? [NC]   
RewriteCond %{QUERY_STRING} (?:^|&)packageType=([^&]+) [NC]
RewriteCond %{QUERY_STRING} !&packageTypeSub=([^&]+)
RewriteRule ^ %1/? [R=301,L]

RewriteCond %{REQUEST_URI}  !(upload-images|email|email-newsletter|login|logout|pml|search|download_pdf|mywebtool|mywebtool_data|mywebtool-enquiries|phpmyadmin|test) [NC]
RewriteRule ^([^/]+)/([^/]+)?$ listings.php?packageType=$1 [L,QSA]

RewriteCond %{REQUEST_URI}  !(upload-images|email|email-newsletter|login|logout|pml|search|download_pdf|mywebtool|mywebtool_data|mywebtool-enquiries|phpmyadmin|test) [NC]
RewriteRule ^([^/]+)/([^/]+)?$ listings.php?packageType=$1&packageTypeSub=$2 [L,QSA] 


#####################################################
# Rewrite specific pages - location (used for all location except provinceState)
RewriteCond %{THE_REQUEST} \ /+location\.php\? [NC]   
RewriteCond %{QUERY_STRING} (?:^|&)place=([^&]+) [NC]
RewriteCond %{QUERY_STRING} !&packageType=([^&]+)
RewriteRule ^ location/%1/? [R=301,L]

RewriteCond %{REQUEST_URI}  !(upload-images|email|email-newsletter|login|logout|pml|search|download_pdf|mywebtool|mywebtool_data|mywebtool-enquiries|phpmyadmin|test) [NC]
RewriteRule ^location/([^/]+)/([^/]+)?$ location.php?place=$1 [L,QSA]

# Rewrite specific pages - location (provinceState)
RewriteCond %{THE_REQUEST} \ /+location\.php\? [NC]   
RewriteCond %{QUERY_STRING} (?:^|&)provinceState=([^&]+) [NC]
RewriteCond %{QUERY_STRING} !&packageType=([^&]+)
RewriteRule ^ location/provinceState-%1/? [R=301,L]

RewriteCond %{REQUEST_URI}  !(upload-images|email|email-newsletter|login|logout|pml|search|download_pdf|mywebtool|mywebtool_data|mywebtool-enquiries|phpmyadmin|test) [NC]
RewriteRule ^location/([^/]+)/([^/]+)?$ location.php?provinceState=$1 [L,QSA]

# Allow place, packageType and packageTypeSub (for dropdowns)
RewriteCond %{THE_REQUEST} \ /+location\.php\? [NC]   
RewriteCond %{QUERY_STRING} (?:^|&)place=([^&]+)&packageType=([^&\s]+)&packageTypeSub=([^&\s]+) [NC]
RewriteRule ^ location/%1/%2/%3/? [R=301,L]

RewriteCond %{REQUEST_URI}  !(upload-images|email|email-newsletter|login|logout|pml|search|download_pdf|mywebtool|mywebtool_data|mywebtool-enquiries|phpmyadmin|test) [NC]
RewriteRule ^location/([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ location.php?place=$1&packageType=$2&packageTypeSub=$3 [L,QSA]

# Allow place, packageType and packageTypeSub (for dropdowns provinceState)
RewriteCond %{THE_REQUEST} \ /+location\.php\? [NC]   
RewriteCond %{QUERY_STRING} (?:^|&)provinceState=([^&]+)&packageType=([^&\s]+)&packageTypeSub=([^&\s]+) [NC]
RewriteRule ^ location/provinceState-%1/%2/%3/? [R=301,L]

RewriteCond %{REQUEST_URI}  !(upload-images|email|email-newsletter|login|logout|pml|search|download_pdf|mywebtool|mywebtool_data|mywebtool-enquiries|phpmyadmin|test) [NC]
RewriteRule ^location/([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ location.php?provinceState=$1&packageType=$2&packageTypeSub=$3 [L,QSA]


#####################################################
# All other pages
RewriteCond %{DOCUMENT_ROOT}/$1\.php [NC]
RewriteRule ^([^/.]+)$ $1.php [L]

# Location of 404 page if errors
ErrorDocument 404 http://example.com/404-not-found

Header add Access-Control-Allow-Origin "*" 

第一部分用于列表页面,第二部分用于位置页面。由于某种原因,位置链接正在传递到列表部分并导致问题。如果我禁用列表部分/部分代码,问题就会消失。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

确实非常奇怪。我假设每次重定向到另一个页面时都会添加额外的正斜杠。解决此问题的方法是强制不显示尾部斜杠。

.htaccess文件中使用此功能:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]

确保在测试之前清除缓存。