简体   繁体   中英

htaccess consolidate root to url and hide index.php

I have these two htaccess rewrites (which I culled from elsewhere) which function independently to do X or Y for my site. What I can't figure out is how to write it so that BOTH functions work properly.

Redirect all requests missing "www":

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Redirect all requests containing "index.php":

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

Additionally, the former function works to rewrite example.com to www.example.com. However, I have another site in a subdirectory on this root with its own "independent" url. But the function also rewrites that url from www.anotherexample.com to www.example.com/anotherexample.

How do I consolidate these into a single workable function without it rewriting my subdirectory site url? Thanks in advance.

You can keep these combined rules in root .htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM