【问题标题】:.htaccess rewrites with some issues.htaccess 重写了一些问题
【发布时间】: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


    【解决方案1】:

    完成.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]
    

    【讨论】:

    • 谢谢 anubhava ,我已经尝试过您的建议,将其替换为您列出的代码,但结果与以前相同。
    • 很有趣,我认为还有其他事情发生,因为我无法打开主域,例如domain.com 最终出现错误,指出“禁止 - 您无权访问此服务器上的 /sections/”。 ,但如果我手动添加 /index .. 有什么想法吗?
    • 是的,同样的 Forbidden You don't have permission to access /sections/ on this server error
    • 好的,尝试在 .htaccess 顶部添加 DirectoryIndex index.php index.html 行。 `
    • 一旦我删除了我在初始帖子中发布的最后 4 行代码,一切正常,这意味着 domain.com 可以正常工作,并且所有扩展都被修剪了,所以肯定有一些东西/sections/ 重写部分有问题
    猜你喜欢
    • 1970-01-01
    • 2014-01-06
    • 2013-07-22
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多