.htaccess - won't add www to only one subdirectory

一曲冷凌霜 提交于 2020-02-25 07:34:46

问题


I have a .htaccess in root directory, with this content:

<IfModule mod_rewrite.c>
    RewriteEngine on
    #RewriteCond %{HTTP_HOST} ^domain.com
    #RewriteRule (.*) http://www.domain.com$1 [R=301,L]
</IfModule>

If I go to http://domain.com/subdir it rewrites to http://www.domain.com/subdir.

But I have a problem with one subdirectory named "crm" - if I go to http://domain.com/crm it redirects me to http://www.domain.com instead of http://www.domain.com/crm.

In this "crm" subdirectory I have another .htaccess file, which rewrites .php extension to .html. Here is the code:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^([^/]*)/([^/]*)\.html$ ?name=$1&id=$2 [L]
    RewriteRule ^(.*).html$ index.php?name=$1 [QSA]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    RewriteRule ^([^/]*)/([^/]*)\.html$ ?name=$1&id=$2 [L]
</IfModule>

Can someone please tell me how can I make this work? So when I will go to http://domain.com/crm it will redirect me to http://www.domain.com/crm.

EDIT:

Root .htaccess was ok, I was changing something and forgot to remove comments for stackoverflow.

crm/.htaccess is now:

RewriteEngine on 
RewriteOptions Inherit 

RewriteRule ^([^/]*)/([^/]*)\.html$ ?name=$1&id=$2 [L] 
RewriteRule ^(.*).html$ index.php?name=$1 [QSA] 

RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule ^ - [L] 
RewriteRule ^([^/]*)/([^/]*)\.html$ ?name=$1&id=$2 [L] 

But it still doesn't redirect this subdir to www.domain.com/crm but only to www.domain.com :(


回答1:


First change root .htaccess to this: (seems to be commented at present)

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Then in crm/.htacess add this line just below RewriteEngine on:

RewriteOptions Inherit

UPDATE: Your crm/.htaccess is faulty. Replace that content with code from below:

RewriteEngine on 
RewriteOptions Inherit 

RewriteRule ^([^/]*)/([^/]*)\.html$ ?name=$1&id=$2 [L,QSA] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^.]+)\.html$ index.php?name=$1 [QSA,L] 

Once this code is there redirect will happen as expected.



来源:https://stackoverflow.com/questions/18980710/htaccess-wont-add-www-to-only-one-subdirectory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!