0

我有一个应该重写我的 URL 的 .htaccess。我的主机告诉我它支持 URL 重写,我通过使用 phpinfo() 和检查验证了这一点。

无论如何,这是我的 .htaccess:

RewriteEngine On

RewriteRule ^([_a-zA-Z0-9]+)$ index.php?page=$1 [R]

它在本地就像一个魅力,但在我的服务器上,它什么也没做。

我之前在互联网上查过,有些人有,但他们都有404错误,而我没有404错误。它根本不重定向,它不做任何事情,所以我收到各种错误消息。

4

2 回答 2

1

RewriteRule ^([_a-zA-Z0-9]+)$ index.php?page=$1 [R]

规则中的正则表达式不匹配任何位置带有斜线的字符串。我不确定这是否可以接受,您也没有给出任何请求示例,但我认为不是。

您可以在根目录的一个 .htaccess 文件中尝试此规则集:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)/?$ index.php?page=$1    [L]

对于永久重定向,将 [L] 替换为 [R=301,L]。

于 2013-05-23T17:40:58.130 回答
0

You can make sure that the file (!-f) or directory (!-d) that you're matching doesn't exist before the rewrite. That way you don't end up with a 500 loop with something like /index.php?page=index. Additionally the ^ character is matching the beginning of the string, so if your original test was in a subdirectory it would not rewrite since you weren't allowing slashes.

This should work for any instance, however it will ONLY make the page variable the last string in the URI.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([_a-zA-Z0-9]+)$ /index.php?page=$1 [R,L]
于 2013-05-24T02:59:17.857 回答