簡體   English   中英

htaccess多個URL變量清理URL

[英]htaccess Multiple URL Variable clean 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/florida

“32746”和“佛羅里達”來自網站索引頁面上的表單,然后通過URL傳遞到“rental”文件夾。

.htaccess代碼sorta可以工作,而它會吐出這樣的URL: http//www.example.com/rentals/12345/arkansas? zipcode = 12345&location =arkansas

但你看到尾端的額外嗎? 就好像它在URL中復制結果一樣。

我目前在我的“ rental ”文件夾中有.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]

因為它描述了來自目的地的原始查詢字符串。

在測試之前清除瀏覽器的緩存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM