
[英]mod_rewrite in .htaccess causing directories to lead to error 500
[英]Mod_rewrite (.htaccess) causing 500 server error when removing extensions (.html) from URL
我一直在寻找在许多主题的溶液(例如1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 )。 不幸的是,我没有找到一个好的解决方案。
总体而言,我想实现以下效果:否www.
在URL没有开始时.html
或/
在URL的末尾,将含有完整路径文件URL(但不显示它的扩展名)。
示例: 用户输入的URL >>重定向(应加载此文件的内容)>>重定向后在地址栏中可以看到的地址
www.example.com
或example.com
或example.com/
或example.com/index.html
或example.com/index
>> index.html >> http://example.com www.example.com/abc
或www.example.com/abc.html
>> abc.html >> http://example.com/abc www.example.com/abc/abc1
或www.example.com/abc/abc1.html
>> abc1.html >> http://example.com/abc/abc1 我服务器上的文件结构:
Main directory:
index.html
abc.html
xyz.html
.htaccess
layout (there are .css, .jpg, .png ... files)
abc:
abc1.html
abc2.html
我的.htaccess文件(版本1):
# rewrite engine initiation
RewriteEngine on
RewriteBase /
#canceling www
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# handle trailing slashes (if not a directory)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R,L]
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_FILENAME} !^.+\.html$
RewriteRule ^(.+[^/])$ $1.html
#301 from example.com/page.html to example.com/page
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
#canceling index or index.html
RewriteRule ^index.(php|html|htm)$ / [R=301,L]
RewriteRule ^([a-z0-9-_]+)/index.(php|html|htm)$ /$1/ [R=301,L]
除以下重定向外,几乎重定向都可以正常工作:
www.example.com/abc.html
>> abc.html >> http://example.com/abc (在地址栏中仍为http://www.example.com/abc.html ) www.example.com/abc/abc1.html
>> abc1.html >> http://example.com/abc/abc1 (在地址栏中仍为http://www.example.com/abc/abc1。 html ) 我的.htaccess文件(版本2):
# rewrite engine initiation
RewriteEngine on
RewriteBase /
#canceling www
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# handle trailing slashes (if not a directory)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R,L]
#canceling index or index.html
RewriteRule ^index.(php|html|htm)$ / [R=301,L]
RewriteRule ^([a-z0-9-_]+)/index.(php|html|htm)$ /$1/ [R=301,L]
RewriteRule ^index$ / [R=301,L]
RewriteRule ^([a-z0-9-_]+)/index$ /$1/ [R=301,L]
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
现在,几乎所有重定向都可以正常工作,但以下重定向除外:
www.example.com/abc
或www.example.com/abc.html
>> abc.html >> http://example.com/abc (我收到403错误-abc是目录,但我也有文件abc。 html) ...并且在几种情况下出现内部服务器错误(500)。 当我在URL末尾添加/
以及任何错误的字符时,就会发生这种情况。
www.example.com/index/anycharacter
www.example.com/abc/abc1/anycharacter
www.example.com/xyz/anycharacter
如果URL的结尾是/
之前的.html
以及任何错误的字符,则会出现404错误。 这是对的。 例子:
www.example.com/index.html/anycharacter
www.example.com/abc.html/anycharacter
www.example.com/abc/abc1.html/anycharacter
www.example.com/xyz.html/anycharacter
我也收到此URL 404错误(这也是正确的,因为它与abc目录有关):
www.example.com/abc/anycharacter
我应该如何更改代码以使一切正常工作? 我已经尝试了很长时间了。 我指望您的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.