如何从.htaccess中的url中删除/ home

时间:2015-05-23 18:22:42

标签: .htaccess clean-urls

我有一个网址: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。我尝试了几次尝试在网上找到的例子,但没有任何效果。希望有人知道答案。

4 个答案:

答案 0 :(得分:0)

尝试:

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

答案 1 :(得分:0)

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

如果您从.htaccess移除上一行,则www.mywebsite.com/home将转到index.php

答案 2 :(得分:0)

您可以在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]

答案 3 :(得分:0)

试试这段代码:

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