Mobile app version of vmapp.org
Login or Join
Connie744

: Redirecting directory without slash to directory with slash and https I'm trying to redirect address: http://example.com/blog to address: https://example.com/blog/ Take into account that, I'm trying

@Connie744

Posted in: #Htaccess #ModRewrite

I'm trying to redirect address:
example.com/blog

to address:
example.com/blog/

Take into account that, I'm trying to redirect from NO HTTPS to HTTPS.

So far, I achieved the desired result in 2 redirects. I want to achieve it in one redirect.

I need also to mention, that blog is a physical directory.

I'm using this rule:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/blog/?$
RewriteRule (.*) www.example.com/blog/ [R=301,L]


It redirects from example.com/blog/ (no-HTTPS) to example.com/blog/ (HTTPS).

And it is .htaccess.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Connie744

2 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

I'm assuming the .htaccess file you are referring to is in the document root? ie. example.com/.htaccess?


It redirects from example.com/blog/ (no-HTTPS) to example.com/blog/ (HTTPS)


Maybe this is just a "typo", but... it doesn't. Your RewriteRule directive explicitly includes the www subdomain.


I achieved the desired result in 2 redirects.


Well, the code you posted is just one redirect, not two. So, where is the other redirect coming from? Ordinarily, mod_dir will trigger an implicit redirect from example.com/blog to example.com/blog/ if blog is a physical directory (which you say it is). However, the directive(s) you posted should override this behaviour, unless...

... You have another .htaccess file in the /blog subdirectory that uses mod_rewrite? If you have a WordPress blog in the /blog subdirectory then this is quite probable. If this is the case then your mod_rewrite directives in the parent .htaccess are being completely ignored - they aren't doing anything! (Which, incidentally, would explain why you have example.com in your RewriteRule directive but are seeing a redirect to example.com - although maybe that is just a "typo"? Please clarify.)

As mentioned, mod_dir will implicitly trigger a 301 redirect from /blog to /blog/ and then maybe WordPress itself is triggering the redirect from HTTP to HTTPS? That's your two redirects. Admittedly that's a bit of a guess, but without more information, that's all we can do. What does the network traffic report? Specifically, what redirects are you seeing?

Solution

If the above hypothesis is correct that it's a bit of a tough one! You could get around the double redirect by moving everything from /blog/.htaccess into the parent .htaccess file (and making the necessary adjustments). However, this might break WordPress to some extent - as WP will no longer be able to maintain the .htaccess file.

Alternatively, if you are on Apache 2.4+ then you can mess with DirectorySlash Off and RewriteOptions AllowNoSlash and move your directives into the /blog/.htaccess file. Although I kinda wonder whether this is really "worth it"?



UPDATE: For example, as mentioned above, you can move your redirect into the /blog/.htaccess file (near the top) and turn DirectorySlash Off and set RewriteOptions AllowNoSlash - this requires Apache 2.4+:

# Prevent mod_dir implicitly appending a slash on directories (via redirect)
DirectorySlash Off

# Allow mod_rewrite to function when there is no trailing slash
RewriteOptions AllowNoSlash

# Redirect example.com/blog to HTTPS
RewriteCond %{HTTPS} off [OR]
RewriteCond %{REQUEST_URI} ^/blog$
RewriteRule ^ www.example.com/blog/ [R=301,L]


This should do as you require - in a single redirect. However, I've modified your redirect to include requests for www.example.com/blog (ie. HTTPS + /blog no trailing slash), otherwise that will no longer be resolved correctly - presumably this is a requirement?

Also, your existing redirect does not redirect www.example.com/blog/<something> - presumably that is intentional?

As with all 301 redirects, your browser cache will need to be cleared before testing.

You should also note that with DirectorySlash Off, mod_dir will not append the trailing slash to any subdirectories - should they be accessed. You will need to manage this as required. This can result in "unexpected behaviour", which I think is the biggest concern. Test test test.

AFAIK the only security concern is if you have directory indexes enabled. So, to be sure, these should be disabled: Options -Indexes in your .htaccess file. See the DirectorySlash directive in the Apache Docs for more information.

10% popularity Vote Up Vote Down


 

@Carla537

Not sure if you meant 'one line' or 'one redirect' (or rewrite in your case). Since you already have only one RewriteRule, I assumed that you are aiming for a one-liner:

RewriteCond %{HTTPS} off
RewriteRule ^/blog/?$ example.com/blog/ [R=301,L]


That rewrites every HTTP-request that starts with /blog and either ends there or has a 'trailing slash' to example.com/blog/

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme