简体   繁体   中英

.htaccess redirect sub-directory to root issue

My main domain is tied to my public_html folder but I use an .htaccess redirect to change the root directory to a sub-directory in the folder to make it easier to host multiple sites. Example:

public_html
-- .htaccess
-- site1
---- .htaccess
-- site2
-- site3

The .htaccess file in the root directory is as follows:

RewriteEngine on 

RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteCond %{REQUEST_URI} !^/subdirectory/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /subdirectory/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteRule ^(/)?$ subdirectory/ [L]

This works fine, but I think there are issues with this and the .htaccess file in my site1 directory. If I enter the URL it sometimes redirects to example.com/site1. So the website appears exactly the same on example.com and example.com/site1 but I don't want it to appear as example.com/site1. The second .htaccess file inside the site1 directory is show below.

RewriteEngine on

# Change default directory page
DirectoryIndex /home/

# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]

# Ensure all directory URLs have a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]

# Same for HTTPS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]

# Redirect default error page to home page
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(/|error/)?$ /home/ [R=301,L]

# Error Documents
ErrorDocument 400 /error/?e=400
ErrorDocument 401 /error/?e=401
ErrorDocument 403 /error/?e=403
ErrorDocument 404 /error/?e=404
ErrorDocument 500 /error/?e=500

# Prevent viewing of htaccess file
<Files .htaccess>
    order allow,deny
    deny from all
</Files>

I'm not sure if it's an issue with the various rewrite rules I'm using that affects the URL, but I would appreciate any help if it can be resolved.

Thanks in advance and I apologise if my description isn't great.

Try

# Ensure all directory URLs have a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [QSA,L,R=301]

# Same for HTTPS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [QSA,L,R=301]

It works for me. %{REQUEST_URI} contains site1, so don't use it. QSA adds any URL parameters like ?param1=val1&param2=val2 If you don't use them, you may omit QSA. Also while testing replace R=301 to R=302 (temporary redirect), else your browser remembers the rule and doesn't send request again.

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