【发布时间】:2016-05-27 04:43:01
【问题描述】:
好的,我已经在我的 .htaccess 文件上尝试了多个版本的代码,但我仍然无法让它正常工作......我不知道我是否将它放在错误的文件夹中或发生了什么但是,这就是我想要完成的。
这是我的代码
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} /index\.php\?zipcode=([^\s&]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([a-z0-9]+)/?$ index.php?zipcode=$1&location=$2 [L,QSA,NC]
这是我尝试过的另一个版本
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?zipcode=([a-z0-9]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /index.php?zipcode=$1&location=$2 [QSA,L,NC]
这是我要完成的任务...一个如下所示的 URL: example.com/rentals/32746/florida
“32746”和“florida”来自网站索引页面上的表单,然后通过 URL 传递到“rentals”文件夹。
.htaccess 代码可以正常工作,但它会吐出这样的 URL: http://www.example.com/rentals/12345/arkansas?zipcode=12345&location=arkansas
但是你看到尾部的额外内容了吗?就好像它在 URL 中复制结果一样。
我目前在“rentals”文件夹中有 .htaccess 文件,它应该在根文件夹中吗?
更新版本
对于那些和我遇到同样问题的人......这是我最终使用的实际代码:
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} /index\.php\?zipcode=([^\s&]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?zipcode=$1&location=$2 [L,QSA,NC]
【问题讨论】:
标签: php apache .htaccess url mod-rewrite