0

目标: 将一个查询字符串转换成一个漂亮的 URL,我从来没有开始工作过。

详细信息: 更改此 URL: http://www.mywebsite.com/static-directory/index.php?state=florida&city=tallahassee

对此: http://www.mywebsite.com/static-directory/florida/tallahassee

我尝试过的: 嗯,几乎所有东西。我最近的搜索想出了这个教程......

我的.htaccess文件在我的根目录(httpdocs)中。

任何帮助或指出我在 Stack Overflow 上正确回答的问题。

谢谢。

4

1 回答 1

2

问题:
“将这个 URL:更改.../static-directory/index.php?state=florida&city=tallahassee
为这个:.../static-directory/florida/tallahassee

根据问题,只需将带有查询的第一个 URL(请求)转换为不带查询的第二个 URL。

这是在根目录的一个 .htaccess 文件中实现此目的的一种方法:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /static-directory/index.php\?state=([^&]+)&city=([^\s&]+) [NC]
RewriteRule  ^static-directory/index.php  /static-directory/%1/%2? [R=301,L,NC]

但是,以防万一 OP 忘记提及整个想法是删除查询以获得“漂亮”的 URL 但仍从原始 URL 获取数据,将接下来的 2 行添加到规则集中:

RewriteCond %{REQUEST_URI}  !index\.php [NC]
RewriteRule  ^static-directory/([^/]+)/([^/]+) /static-directory/index.php?state=$1&city=$2 [L]
于 2013-05-28T03:18:53.530 回答