简体   繁体   English

htaccess多个URL变量清理URL

[英]htaccess Multiple URL Variable clean URL

Ok i've tried multiple versions of code on my .htaccess file and I still can't get it to work properly... I don't know if I'm putting it in the wrong folder or what is going on but, here is what I'm trying to accomplish. 好吧,我已经在我的.htaccess文件上尝试了多个版本的代码,我仍然无法让它正常工作......我不知道我是将它放在错误的文件夹中还是发生了什么但是,这是我想要完成的。

This is my code 这是我的代码

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]

Here is another version that I've tried 这是我尝试过的另一个版本

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]

Here is what I'm trying to accomplish... a URL that looks like this: example.com/rentals/32746/florida 这是我想要完成的...一个看起来像这样的URL: example.com/rentals/32746/florida

The "32746" and the "florida" come from a form on the index page of the site and then it gets passed over to the "rentals" folder through the URL. “32746”和“佛罗里达”来自网站索引页面上的表单,然后通过URL传递到“rental”文件夹。

The .htaccess code sorta works whereas it spits out a URL like this: http://www.example.com/rentals/12345/arkansas?zipcode=12345&location=arkansas .htaccess代码sorta可以工作,而它会吐出这样的URL: http//www.example.com/rentals/12345/arkansas? zipcode = 12345&location =arkansas

But do you see the extra on the tail end? 但你看到尾端的额外吗? It's as if it's duplicating the results in the URL. 就好像它在URL中复制结果一样。

I currently have the .htaccess file in my " rentals " folder should it be in the root folder? 我目前在我的“ rental ”文件夹中有.htaccess文件,如果它在根文件夹中?


UPDATED VERSION 更新后的版本

For those who were running into the same problem as myself... here is the actual code I ended up going with: 对于那些和我一样遇到同样问题的人来说......这是我最终得到的实际代码:

 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] 

You need to add a ? 你需要添加一个 to the end of the target path in your first Rule : 到第一个规则中目标路径的末尾:

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]

As it descards the orignal query strings from the destination. 因为它描述了来自目的地的原始查询字符串。

Clear your browser's cache before testing this. 在测试之前清除浏览器的缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM