使用 mod_rewrite 将所有流量重定向到 index.php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8595964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:03:21  来源:igfitidea点击:

Redirect all traffic to index.php using mod_rewrite

phpmod-rewriteapache

提问by bswinnerton

I'm trying to build a url shortener, and I want to be able to take any characters immediately after the domain and have them passed as a variable url. So for example

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

would become

会成为

Here's what I have for mod_rewrite right now, but I keep getting a 400 Bad Request:

这是我现在对 mod_rewrite 的要求,但我不断收到 400 Bad Request:

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

回答by Rocket Hazmat

Try replacing ^(.*)with ^(.*)$

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

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

Edit: Try replacing index.phpwith /index.php

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

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

回答by nassim

ByTheWay don't forget to enable mod rewrite,in apache on ubuntu

ByTheWay 不要忘记在 ubuntu 上的 apache 中启用 mod rewrite

sudo a2enmod rewrite 

and then restart apache2

然后重启apache2

sudo /etc/init.d/apache2 restart

edit: it is an old question that helped me but I lost hours testing my apache2.conf file on one side and .htaccess file on the other side and still no light.

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

回答by starkeen

To rewrite all requests to /index.php,you can use :

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

RewriteEngine on


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

The RewriteCondition RewriteCond %{REQUEST_URI} !^/index.php$is important as it excludes the rewrite destination we are rewriting to and prevents infinite looping error.

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

回答by PseudoNinja

Try it without using using RewriteCond.

在不使用 using 的情况下尝试一下RewriteCond

回答by bfpetersjr

Check under Apache modules and make sure that rewrite-module is enabled. I had a similar issue and it was resolved by enabling the rewrite-module.

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