【问题标题】:htaccess Multiple URL Variable clean URLhtaccess 多个 URL 变量清除 URL
【发布时间】: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


    【解决方案1】:

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

    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]
    

    因为它会从目标中删除原始查询字符串。

    在测试之前清除浏览器的缓存。

    【讨论】:

    • 它现在只是转到 example.com/rentals/index.php?zipcode=32827&location=florida 我复制了您提供的代码,替换了 .htaccess 文件中的代码然后将其上传到 /rentals/ 文件夹并从 Firefox(而 chrome 是原始浏览器)对其进行测试,知道是什么原因造成的吗?
    • @Antivinegar 是的,问题是,RewriteCond 正在尝试匹配根文件夹的 /index.php。从条件中删除 ^[A-Z]{3,}\s+
    • 感谢您的帮助...它可以将 URL 切换为我想要的格式,但是当显示 URL 时,好像没有任何 CSS 是完整的,并且页面显示为好像 CSS当我知道它不是在实现 .htaccess 更改之前页面工作时,它就坏了
    • @Antivinegar 要修复 css、js 和其他相关链接,您需要在网页的 head 部分添加基本标签 或使用绝对路径,例如 而不是
    • 即使那不起作用...我也尝试直接链接它们,但仍然不起作用。看到我只创建了一个名为“rentals”的文件夹,“Zipcode”和“location”只是来自一个表格的输入,该表格被转移到“/rentals/index.php”我也试过这个代码并且它工作得很好。 .. 除了“位置”没有传入 URL RewriteEngine On RewriteBase /rentals/ RewriteCond %{THE_REQUEST} /index\.php\?zipcode=([^\s&]+) [NC] RewriteRule ^ %1? [R=301,L,NE] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^/]+)/?$ index.php?zipcode=$1 [L,QSA,NC]
    猜你喜欢
    • 2011-06-23
    • 2013-03-04
    • 1970-01-01
    • 2013-05-12
    • 2013-12-24
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    相关资源
    最近更新 更多