[英]Issue with mod_rewrite when accessing directory
我的代码:
RewriteEngine on
RewriteBase /
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ index.php?page=$1 [L]
我正在使用WAMP并设置了虚拟主机。 在我的index.php中,有代码可以让页面通过并检查页面是否存在(在数据库中)。 如果不是,则显示错误消息。 工作正常。
例如: http://mysite/contactus/
但是,如果我在URL中使用aa目录名作为page_name,它将无法正常工作。 例如: http://mysite/images/
。 这将显示页面未找到错误(即,检查数据库并且找不到页面,因此显示“未找到”)。 但它不会在页面中显示图像,css(链接文件)。 此外,它在地址http://mysite/images/?page=images
显示http://mysite/images/?page=images
。
这样,如果我转到用于存储javascript文件的js
文件夹,则会发生上述问题。 因此,如果将任何子目录的名称作为页面名传递,则会引起问题。
如何解决呢?
提供http://mysite/images/
,mod_rewrite将重定向到http://mysite/images/index.php?page=images
而不是http://mysite/index.php?page=images
编辑请告诉我如何阻止文件和目录的热链接,并重定向回索引页或发送一些浏览器标题错误?
我尝试了这个:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*) http://%{HTTP_HOST} [R,L]
RewriteRule ^article/([a-z]+)/?$ /index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L]
编辑新代码(半工作):
RewriteEngine on
RewriteBase /
# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1/ [R,L]
RewriteRule ^article/([a-z]+)/?$ http://%{HTTP_HOST}/index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ http://%{HTTP_HOST}/index.php?page=$1 [L]
当提到目录名称时,此代码将解决不显示图片和CSS的问题。 但是无论我指定什么页面名称,例如: http://mysite/contactus
,它都将转到URL: http://mysite/index.php?page=contactus
。 即使我使用目录名称,例如: http://mysite/js
,它也会转到: http://mysite/index.php?page=js
我很困扰。
RewriteEngine on
RewriteBase /
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/*$ /index.php?page=$1 [L]
您必须将斜线放在前面。
编辑:更改了? 至 *
我的理解是您的脚本仅用于文档,而不用于图像或其他资源。
然后,您应该立即忽略它们。 尝试像这样在RewriteBase
之后RewriteBase
添加此行:
RewriteEngine on
RewriteBase /rewrite/
RewriteRule ^/(images|js)/(.*)$ - [L]
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ index.php?page=$1 [L]
然后将立即为这些子目录提供服务,从而绕过下一个RewriteRule
集。
对于目录问题,我通常会强制使用斜线
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+[^/])$ $1/ [R]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.