阿帕奇mod_rewrite问题


Apaches mod_rewrite issues

我在使用apaches mod_rewrite时遇到了问题。我想用我的 php 应用程序制作干净的网址,但它似乎没有给出我期望的结果。

我在我的 .htaccess 文件中使用此代码:

RewriteEngine on
RewriteRule ^project/([0-9]{4})/([0-9]{2})$ /project/index.php?q=$1&r=$2 [L]
RewriteRule ^project/([0-9]{4})$ /project/index.php?q=$1 [L]
http://localhost/user/project/system,当我

观看时,要做到这一点,它将等同于观看http://localhost/user/project/index.php?q=system

我没有得到任何结果,而不是得到一个典型的 404 错误。

我也刚刚检查了mod_rewrite是否有效,将我的 .htaccess 代码替换为以下内容:

Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*) http://www.stackoverflow.com

它正确地将我重定向到这里,所以mod_rewrite肯定是有效的。

我的项目的根路径是/home/user/public_html/project

用于查看我的项目的网址是http://localhost/user/project

如果需要更多信息,请告诉我。

谢谢

如果你的.htaccess文件确实已经位于project/子目录中,那么不要再在重写规则中提及它。删除它:

RewriteRule ^([0-9]{4})/([0-9]{2})$ /project/index.php?q=$1&r=$2 [L]
# no "project/" here

规则始终与当前本地文件名映射相关。

否则尝试使用RewriteBase。

您的

正则表达式中有 [0-9]{4},它只会匹配 4 位数字。但是,"系统"不是 4 位数字,因此不匹配。

您可以改用类似 [^/]+ 的内容。

RewriteRule ([^/]+)/([0-9]{2})$ /index.php?q=$1&r=$2 [L]
RewriteRule ([^/]+)$ /index.php?q=$1 [L]

不知道第二个参数是否应该是 2 位数字的数字。

编辑:我现在还在开头添加了"用户"。

编辑2:好的,我以为你在你的htaccess的根htdocs中。因此,如果您在带有 .htaccess 的"项目"中,请删除"项目"和"用户"。

你可能的意思

RewriteRule ^/project/([0-9]{4})/([0-9]{2})$ /project/index.php?q=$1&r=$2 [L] 
RewriteRule ^/project/([0-9]{4})$ /project/index.php?q=$1 [L] 
"

^project"的意思是"行的开头是'项目'",但开头是"/project",因此您需要包含起始斜杠(即"^/project...")。


抱歉,错过了系统位(和用户位)。全神贯注于斜杠。

RewriteRule ^/user/project/([a-zA-Z0-9]*)/([a-zA-Z0-9]*)$ /user/project/index.php?q=$1&r=$2 [L] 
RewriteRule ^/user/project/([a-zA-Z0-9]*)$ /user/project/index.php?q=$1 [L] 

应该有你的权利。