3

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

我在我的 .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

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

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

谢谢

4

3 回答 3

0

You have [0-9]{4} in your regex which will only match numbers of 4 digits. "system", however, is not a number of 4 digits, and therefore does not match.

You can use something like [^/]+ instead.

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

Don't know if the second parameter should be a number with 2 digits or not.

Edit: I also added "user" at the beginning now.

Edit2: Okay, I thought you were in the root htdocs with your htaccess. So remove "project" and "user" if you are in "project" with the .htaccess.

于 2012-06-03T10:41:59.963 回答
0

你大概是说

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',所以你需要包括起始斜杠(即'^/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] 

应该有你的权利。

于 2012-06-03T10:30:34.413 回答
0

如果您的.htaccess文件确实已经位于project/子目录中,则不要在 RewriteRule 中再次提及它。去掉它:

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

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

其他实验RewriteBase

于 2012-06-03T10:55:39.727 回答