【发布时间】:2015-03-08 16:18:29
【问题描述】:
我正在建立一个网站,但我发现我遇到了这个问题。
当用户访问 www.website.com/something 我希望 htaccess 打开 www.website.com/index.php?s =something ,这适用于我当前的重定向,除非我将“某物”作为现有文件夹。
“s”代表“子页面”
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /index.php?s=$1 [L]
只有当文件夹存在时我才会遇到问题 ( /js /css /images ..etc ) 所以当我尝试访问 www.website.com/images 它仍然显示正确的页面但是它在地址栏中显示此内容 www.website.com/images/?s=images 。需要注意的一件事是,如果我将 ?s=images 更改为 ?s=images213123 php 仍然只能将 /images/ 识别为 $_GET['s'];
我没有使用 htaccess 的经验,但这是我到目前为止汇总的内容,也许问题出在其他地方,而不是在上面的行中。 我尝试将 [L] 移至第三条规则,尝试将有问题的规则与其他规则分开,在我有限的知识范围内尝试了所有可能的方法,但我无法获得任何效果。
非常感谢任何提示或想法! 提前谢谢你
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^u/([a-zA-Z0-9_-]+)$ /u.php?u=$1
RewriteRule ^p/([a-zA-Z0-9_-]+)$ /$1.php
RewriteRule ^([a-zA-Z0-9_-]+)/userpage/([a-zA-Z0-9_-]+)/?$ /c.php?s=$1&c=$2
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /index.php?s=$1 [L]
ErrorDocument 400 /index.php
ErrorDocument 401 /index.php
ErrorDocument 403 /index.php
ErrorDocument 404 /index.php
ErrorDocument 500 /index.php
编辑:添加“DirectorySlash Off”并更改@Jon Lin 回答中的条件和规则解决了这个问题。我还必须清除 Firefox 缓存才能看到更改。这是我的最后一个 htaccess,可以很好地满足我的目的。
Options +FollowSymlinks
RewriteEngine On
DirectorySlash Off
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^u/([a-zA-Z0-9_-]+)$ /u.php?u=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([a-zA-Z0-9_-]+)$ /$1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/userpage/([a-zA-Z0-9_-]+)/?$ /c.php?s=$1&c=$2
# I intentionally skipped the conditions here
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /index.php?s=$1 [L]
ErrorDocument 400 /index.php
ErrorDocument 401 /index.php
ErrorDocument 403 /index.php
ErrorDocument 404 /index.php
ErrorDocument 500 /index.php
【问题讨论】:
标签: .htaccess mod-rewrite