.htaccess重写了一些问题

时间:2014-01-12 12:54:02

标签: regex .htaccess mod-rewrite redirect rewrite

我对当前的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)应删除,我看到一个页面文本永久移动 - 文档已移至此处

我目前的.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

1 个答案:

答案 0 :(得分:0)

完成.htaccess:

 Options +FollowSymLinks -MultiViews -indexes
 RewriteEngine On
 RewriteBase /

 RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
 RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

 RewriteCond %{THE_REQUEST} \s/+sections/(.+?)(?:\.php)?[/?\s] [NC]
 RewriteRule ^ %1 [R=301,L]

 # remove index
 RewriteCond %{THE_REQUEST} /index(\.php)?[\s?/] [NC]
 RewriteRule ^(.*?)index(/|$) /$1 [L,R=301,NC,NE]

 # 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]