【问题标题】:Combine .htaccess rules: remove extension and rewrite URL variables结合 .htaccess 规则:移除扩展名并重写 URL 变量
【发布时间】:2013-09-27 12:14:20
【问题描述】:

我有一个.htaccess 文件,其中包含以下规则

# replace 'RewriteBase' with current working folder
# put '<base href="/">' in <head> if you are in the root folder
# put '<base href="/foldername/">' in <head> if you are in a subfolder of the root
Options +FollowSymLinks
RewriteEngine On
RewriteBase /pretty-urls

# Remove slash if file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1 [R=301,L]

# Redirect to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ user.php?u=$1 [NC]

这基本上将以下任何 URL、www.mysite.com/about.phpwww.mysite.com/aboutwww.mysite.com/about/ 转换和重定向到 www.mysite.com/about,并且还应该允许我写 www.mysite.com/user/john,但会引发内部服务器错误加上 500。

不知道哪里出了问题,代码就是基于这个(How-to? Vanity URL & ignore .PHP extension with .htaccess

我的 php 是这样的

foreach ( $users as $user ) {
    echo '<p><a href="user/'.$user->username.'">'.$user->username.'</a></p>';
}

感谢您的帮助,谢谢。

编辑:Apache 错误日志说

[Fri Sep 27 14:26:26.539589 2013] [core:error] [pid 2276:tid 1680] [client ::1:60023] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://localhost/pretty-urls/users

按照建议,我删除了[NC] 标志并添加了[L] 标志,但同样的错误仍然存​​在。

【问题讨论】:

  • 尝试使用 Apache 日志调试您的重写脚本,如下所示:serverfault.com/questions/135694/…
  • 谢谢,我检查了答案并更新了我的帖子,但仍然出现 500 错误。
  • 您在extensionless url 规则中缺少]
  • 抱歉,打错字了,在文件中是正确的

标签: php .htaccess url-rewriting vanity-url


【解决方案1】:

错误Request exceeded the limit of 10 internal redirects通常是由既匹配原始url又匹配重写url的重写规则引起的。 L 标志只会停止当前周期的重写,但是当 apache 再次通过 .htaccess 推送请求时,这不会停止这些错误的内部重定向。

在我自己的本地主机上重新创建它时,我注意到问题在于将.php 添加到您的网址的规则。

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

%{REQUEST_FILENAME} 在我的例子中包含document_root/user 的文件路径。您测试document_root/user.php 是否是一个文件(它是),然后将.php 添加到url,这将导致/user/john/.php。我认为可以解决问题,将文件移动到子目录,这样虚拟 url 将永远不会匹配文件夹中的文件或目录。

您可以将规则更改为以下内容,但我不确定这是否会完全消除问题:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?(.*)$ $1.php?q=$2 [L]

这会将link.url/test/qwer 重写为test.php?q=qwerlink.url/testtest.php?q=

您为user.php 进行内部重写的规则应移至“一般”规则之上,并应更改为:

RewriteRule ^user/(.*)/?$ user.php?u=$1 [NC,L]

这会将link.url/user/john/ 重写为user.php?u=john

一如既往,请参阅the documentation。要正确调试.htaccess,请将LogLevel alert rewrite:trace3 放入您的httpd.conf 并重新启动服务。这将显示如下行,这可以让您了解正在发生的事情:

[rewrite:trace1] [pid 4800:tid 1532] mod_rewrite.c(468): [client ::1:49451] ::1 - - [localhost/sid#3ecb48][rid#12b1a88/initial/redir#2] [perdir C:/wamp/www/] internal redirect with /user/john/.php.php.php [INTERNAL REDIRECT]

【讨论】:

  • 你居然解决了,不知道怎么感谢你。我倒置了最后两个块,现在一切正常。再次感谢您!
猜你喜欢
  • 2012-03-18
  • 1970-01-01
  • 1970-01-01
  • 2012-12-24
  • 1970-01-01
  • 2017-01-07
  • 2022-01-02
  • 2011-08-29
  • 1970-01-01
相关资源
最近更新 更多