【发布时间】:2014-01-13 15:58:43
【问题描述】:
我当前的 htaccess 文件(重写)有一些问题,如果有人能帮助我解决这个问题,我将不胜感激。
这是我当前的 htaccess 文件:
Options +FollowSymLinks -MultiViews -indexes
RewriteEngine On
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+sections/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^sections/)^(.*)$ /sections/$1 [L,NC]
在我的 public_html 文件夹中,我有一个名为sections 的子文件夹,它本身包含几个其他子文件夹。我想要实现的是使用我上面发布的配置实际隐藏 /sections/ 文件夹,该文件夹在一定程度上起作用,但是我仍然遇到一些问题。
如果我要打开 www.mydomain.com/sections/file 它会成功重定向到 www.mydomain.com/file - 这已经很好了,但是如果我尝试添加 .php 扩展名(仅用于测试目的)重定向有效(部分文件夹被跳过),但是文件扩展名没有被修剪(我仍然看到 .php)应该被删除并且我看到一个页面文本 Moved Permanently - 文档已移至此处...
在我当前的 .htaccess 文件中我可以做/修改什么或不正确的事情吗?一些专家的建议将不胜感激。
非常感谢!
按要求更新:
DirectoryIndex index.php index.html
Options +FollowSymLinks -MultiViews -indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{THE_REQUEST} \s/+sub/(.+?)\.php [NC]
RewriteRule ^ %1 [R=301,L]
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [L,R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=302]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301,L]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^sections/)^(.*)$ /sections/$1 [L,NC]
AuthType Basic
AuthName "xxx"
AuthUserFile "/home/xxx/.htpasswds/public_html/passwd"
require valid-user
【问题讨论】:
标签: regex .htaccess mod-rewrite redirect rewrite