使用mod_rewrite将所有stream量redirect到index.php

我正在尝试构build一个url缩短器,我希望能够在域之后立即获取任何字符,并将它们作为variablesurl传递。 所以例如

  • http://google.com/asdf

会成为

  • http://www.google.com/?url=asdf 。

这里是我现在mod_rewrite,但我不断收到400坏请求:

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*) index.php?url=$1 [L,QSA] 

尝试用^(.*) ^(.*)$替换^(.*)

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

编辑:尝试用/index.php替换index.php

 RewriteRule ^(.*)$ /index.php?url=$1 [L,QSA] 

ByTheWay不要忘记启用mod重写,在Ubuntu的Apache

 sudo a2enmod rewrite 

然后重新启动apache2

 sudo /etc/init.d/apache2 restart 

编辑:这是一个老问题,帮助我,但我失去了一个小时测试我的apache2.conf文件和另一侧的.htaccess文件,仍然没有灯光。

尝试一下,不要使用RewriteCond

要将所有请求重写到/index.php ,可以使用:

 RewriteEngine on RewriteCond %{REQUEST_URI} !^/index.php$ RewriteRule ^(.+)$ /index.php?url=$1 [NC,L] 

RewriteCondition RewriteCond %{REQUEST_URI} !^/index.php$非常重要,因为它排除了我们正在重写的重写目标,并防止了无限循环错误。

检查Apache模块下,并确保重写模块已启用。 我有一个类似的问题,并通过启用重写模块解决。