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

2019-08-08 15:24发布

问题:

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.