2013-09-27 83 views
1

我有一个.htaccess文件下列规则结合的.htaccess规则:删除扩展和重写URL变量

# 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] 

这基本上转换和重定向以下任一网址,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]标志,但同样的错误仍然存​​在。

+0

尝试调试使用Apache日志你重写剧本,像这样: http://serverfault.com/questions/135694/mod-rewrite-settings-causes-server-to-throw-http-500-errors-404- –

+0

谢谢,我检查了答案并更新了我的帖子,但还是500错误。 –

+0

你在''extensionless url''规则中缺少']'。 – anubhava

回答

2

错误Request exceeded the limit of 10 internal redirects通常是由重写规则引起的,它们都与原始url以及重写的url相匹配。 L标志只会停止当前周期的重写,但由于apache无论如何都会通过.htaccess推送请求,因此这不会阻止这些错误的内部重定向。

当我在自己的本地主机上重新创建这个时,我注意到问题是将.php添加到您的url的规则。

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/qwertest.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] 
+0

你实际上解决了它,不知道如何谢谢你。我倒过去的两个街区,现在一切正常。再次感谢你 ! –