htaccess多个URL变量干净URL

好的,我已经在.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/佛罗里达

“ 32746”和“ florida”来自站点索引页上的表单,然后通过URL传递到“ rentals”文件夹中.

.htaccess代码sorta可以工作,而它会发出如下URL:
http://www.example.com/rentals/12345/arkansas?zipcode=12345&location=arkansas

但是,您在尾部看到额外的东西了吗?就像是在URL中复制结果一样.

我的“租借”文件夹中目前有.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]

解决方法:

您需要添加一个?到第一个规则中目标路径的末尾:

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]

因为它丢弃了来自目的地的原始查询字符串.

在测试之前,请清除浏览器的缓存.

上一篇:php-子文件夹的htaccess重写规则(漂亮的url)


下一篇:php-Laravel路线无法在生产中使用