mod_rewrite使第一级子目录是一个GET变量

时间:2012-08-28 21:45:57

标签: .htaccess codeigniter mod-rewrite url-rewriting

好吧,标题真的很草率。

这是我的问题:我有一个新闻网站,当你转到主页面(domain.com)时,它会在你弄清楚你的地理位置后重定向到domain.com/news/top?geography=San_Francisco

如何使用.htaccess使其来自domain.com/news/top?geography=San_Francisco domain.com/San_Francisco/news/top

有一些类似的问题,但我没有找到一个类似的问题,因为你正在将URL编辑为otherback子目录。

还应该注意的是,我正在使用PHP的Code Igniter框架,它通常将它作为domain.com/index.php/news/top?geography=San_Francisco但我已经做了一个mod_rewrite来摆脱index.php。代码如下:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

我试过的代码:

RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

1 个答案:

答案 0 :(得分:0)

在您拥有的index.php规则之前,请尝试添加以下内容:

RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]

您需要确保您生成的链接的格式为domain.com/San_Francisco/news/top


但是为了处理仍然看起来像旧方式的野外链接,你必须与实际请求相匹配:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news/top\?geography=([^&]+)
RewriteRule ^news/top$ /%1/news/top? [L,R=301]

如果有人访问链接domain.com/news/top?geography=San_Francisco并将其设置为浏览器的地址栏,则会重定向浏览器:domain.com/San_Francisco/news/top。此时浏览器将发送另一个第二个URL请求,并使用上面的规则将其更改回带有查询字符串的规则。