.htaccess中的URL RewriteRule用于index.php查询参数

时间:2014-02-18 13:14:09

标签: apache .htaccess mod-rewrite redirect url-rewriting

示例网址: -

  • www.domain.com/index.php?bob
  • www.domain.com/index.php?jane
  • www.domain.com/index.php?fred

需要重写,如: -

  • www.domain.com/bob
  • www.domain.com/jane
  • www.domain.com/fred

现在尝试了很多变化,但我能得到的最接近的是: -

  • www.domain.com/?bob
  • www.domain.com/?jane
  • www.domain.com/?fred

下面的.htaccess实现了这个......

RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

请有人指出我需要修改哪些内容? (问号)在URL?

修改

注意到,自从应用anubhava下面给出的回答后,例如robots.txt无法解析为.txt文件,只显示主页。

.htaccess如下: -

RewriteEngine On

RewriteBase /
RewriteCond %{THE_REQUEST} \s/+index\.php\?([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]

RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#pos1
RewriteRule ^([^/]+)/?$ index.php?$1 [L,QSA] <--

RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

#pos2

如果我在pos1中添加&lt; - 行,则robots.txt网址会返回404找不到页面错误。

如果我在pos2中添加&lt; - 行,robots.txt网址只会显示主页。

EDIT2

与此同时,我通过在robots.txt下添加以下内容,将Rewrite Base /排除在重写之外: -

RewriteRule ^robots.txt - [L]

2 个答案:

答案 0 :(得分:3)

您可以使用此代码:

RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?$1 [L,QSA]

答案 1 :(得分:0)

您也可以使用:

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f 

RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]