Reputation: 17775
On my shared Apache hosting, I'm attempting to seamlessly redirect (without rewriting the URL in the client's browser) all requests to my website to a subdirectory named /blog
. I'm doing this with a .htaccess
file.
This is what I have so far (from here):
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{HTTP_HOST} ^example\.
RewriteRule ^(.*)$ /blog/$1 [L]
This works fine for URLs with a trailing slash.
For example when the user accesses https://example.com/about/
, the files from http://example.com/blog/about/
are served, and the URL remains https://example.com/about/
.
Problem
When the user accesses https://example.com/about
(no trailing slash), the files from https://example.com/blog/about/
are served, but the URL in the browser changes to https://example.com/blog/about/
. The URL change is not desired.
Question
How can I alter my .htaccess
file to ensure that the URL in the client's browser does not change when the trailing slash is missing?
Additional Info
The only option I have checked in my provider's back-end is "force SSL". There are no other .htaccess
files present on the server.
Upvotes: 1
Views: 138
Reputation: 785581
Trailing slash is being added by Apache's mod_dir
module as /blog/about/
is a real directory and trailing slash is added in front of directories to prevent directory listing.
You may use these rules:
DirectorySlash Off
Options -Indexes
RewriteEngine On
# add a trailing slash if desrtination is a directory
RewriteCond %{DOCUMENT_ROOT}/blog%{REQUEST_URI} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^example\. [NC]
RewriteRule !^blog/ blog%{REQUEST_URI} [NC,L]
Make sure to test this in a new browser to avoid old cache.
Upvotes: 1