最近我修改了apache服务器中的.htaccess
文件。这是下面的代码
RewriteEngine On
<IfModule mod_speling.c>
CheckSpelling On
</IfModule>
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^course/([^/.]+)$ course.php?name=$1 [QSA,L]
RewriteRule ^course/([^/.]+)/$ course.php?name=$1 [QSA,L]
RewriteRule ^about$ about.php [QSA,L]
RewriteRule ^about/$ about.php [QSA,L]
RewriteRule ^contact$ contact.php [QSA,L]
RewriteRule ^contact/$ contact.php [QSA,L]
RewriteRule ^downloads$ downloads.php [QSA,L]
RewriteRule ^downloads/$ downloads.php [QSA,L]
RewriteRule ^ errors/404.html [L,NC]
现在的问题是如果我在web浏览器中输入类似http://localhost/about
的URL,它总是显示自定义的404.html
我认为问题出在最后一行。
如果我去掉最后一行,它就会起作用。但我也想要404页。类似于如果用户输入http://localhost/SomeRandomString
,404页面将显示在此url上。
我也在.htaccess文件上尝试了ErrorDocument
,但它会更改的url
有解决这个问题的办法吗。
按照以下方式创建htaccess规则文件。将其放在根目录中,确保在检查URL之前清除浏览器缓存。
RewriteEngine On
<IfModule mod_speling.c>
CheckSpelling On
</IfModule>
##Rules that cover your uris starting with course here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(course)/([^/.]+)/?$ $1.php?name=$2 [NC,QSA,L]
##Rules that cover your uris starting with about/contact/downloads here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(about|contact|downloads)/?$ $1.php [NC,QSA,L]
##Rules that cover all uris which aren't matching any of the above conditions.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ errors/404.html [L,NC,QSA]