无法在.htaccess中为我的php网站编写重写规则

时间:2014-01-26 04:55:33

标签: php regex apache .htaccess mod-rewrite

我的网站是v2.example.com,我正在尝试编写.htaccess规则但无法使用。

我想要这个:v2.example.com/ABCDEFGH

我希望将值ABCDEFGH作为参数,例如v2.example.com/index.php?id=ABCDEFGH

任何人都可以帮我解决这个问题。

我试过了:

<IfModule mod_rewrite.c>
RewriteEngine On 
RewriteBase /
RewriteRule ^(.*) index.php?id=$1 [L]
</IfModule>

请帮帮我。

2 个答案:

答案 0 :(得分:1)

RewriteEngine on
RewriteRule ^(.*)$  index.php?id=$1 [L,QSA]

这会在进入无限循环时崩溃,因为index.php会进一步重写为index.php?id = index.php等等

溶液

RewriteEngine on
RewriteRule ^([^_]*)$   _index.php?id=$1 [L,QSA]

即创建页面_index.php,并将所有没有_的网页重写到此页面,这样它就不会进入无限循环,您可以选择~而不是{{ 1}}如果您认为在参数中需要_

答案 1 :(得分:1)

你的规则如下:

<IfModule mod_rewrite.c>
RewriteEngine On 
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?id=$1 [L,QSA]
</IfModule>