【问题标题】:How to remove /home from url in .htaccess如何从 .htaccess 中的 url 中删除 /home
【发布时间】:2015-05-23 18:22:42
【问题描述】:

我有一个网址:www.mysite.com,通过 .htaccess 获得干净的网址,代码如下:

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^ index.php [L]
</IfModule>

除了我希望 www.mysite.com/home 转换为 www.mysite.com 之外,一切都运行良好。基本上我想删除/home。我用网上找到的例子尝试了几次尝试,但没有任何效果。希望有人知道答案。

【问题讨论】:

    标签: .htaccess clean-urls


    【解决方案1】:

    试试:

    <IfModule mod_rewrite.c>
     RewriteEngine on
    RewriteCond %{REQUEST_URI} ^/home/?$ [NC]
    RewriteRule ^ http://example.com/ [L,R]
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^ index.php [L]
    </IfModule>
    

    【讨论】:

      【解决方案2】:

      问题在于RewriteCond %{REQUEST_FILENAME} !-d 行告诉服务器,如果请求实际存在的目录,则不要重写。

      如果您从.htaccess 中删除上面的那一行,那么www.mywebsite.com/home 将导致index.php

      【讨论】:

        【解决方案3】:

        您可以在 root .htaccess 中尝试此代码:

        RewriteEngine on
        
        RewriteCond %{THE_REQUEST} /home [NC]
        RewriteRule ^ / [L,R=302]
        
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^ index.php [L]
        

        【讨论】:

          【解决方案4】:

          试试这个代码:

          <IfModule mod_rewrite.c>
           RewriteEngine on
          
           # Redirect from http://domain.com/home/... to http://domain.com/...
           RewriteRule ^home\/(.*)$ /$1 [L,R=301]
           RewriteRule ^home\/?$ / [L,R=301]
          
           RewriteCond %{REQUEST_FILENAME} !-f
           RewriteCond %{REQUEST_FILENAME} !-d
           RewriteRule ^ index.php [L]
          </IfModule>
          

          否则,如果您打算将网站的所有内容从根目录放入子文件夹,请尝试以下代码:

          <IfModule mod_rewrite.c>
           RewriteEngine on
          
           # Point from http://domain.com/... to http://domain.com/home/...
           RewriteCond $1 !^home
           RewriteRule ^(.*)$ /home/$1 [L]
          
           RewriteCond %{REQUEST_FILENAME} !-f
           RewriteCond %{REQUEST_FILENAME} !-d
           RewriteRule ^ /home/index.php [L]
          </IfModule>
          

          【讨论】:

            猜你喜欢
            • 2011-05-20
            • 2017-03-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多