2

When I type my website zeugmarket.com it comes out as https://zeugmarket.com//

I wrote this htaccess file to move from HTTP to https but it is also adding two slashes at the end. How do I stop it:

RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://www.zeugmarket.com%{REQUEST_URI} [L,R=301]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
ridertiger
  • 33
  • 7

2 Answers2

3
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://www.zeugmarket.com%{REQUEST_URI} [L,R=301]

There would seem to be "something else" that is adding the additional slash - it's not happening because of these directives.

at the end of my website URL like this: //

The double slashes are actually at the start of the URL-path, not strictly at the end. eg. If you request http://example.com/foo then it returns https://example.com//foo.

However, this redirect response is actually coming from an Nginx server, not Apache. Nginx is probably being used as a front-end (reverse) proxy - hence the requirement to use ENV:HTTPS (environment variable) and not HTTPS (server variable) in the RewriteCond directive.

I would look for a config issue with the Nginx proxy, as it's most probably this that is adding the additional slash.

RewriteRule ^(.*)$ https://www.zeugmarket.com%{$1} [L,R=301]

Incidentally, in this example (from your comment) %{$1} is not strictly valid and will always result in an empty string - so the double slash is definitely not coming from Apache.

However, you appear to have a single-page-website. You do not have any URL-path on any URLs? There is only one visible URL, and that's https://www.example.com/, so your directives can be simplified:

RewriteCond %{ENV:HTTPS} !on
RewriteRule ^ https://www.example.com/ [L,R=301]

Note that there should be a slash after the hostname (at the start of the URL-path). If you omit it then (ordinarily) the browser will append the slash in the redirected request. (But it seems that Nginx is actually appending these slash(es) in the redirect response - before it gets to the browser.)

For a bit more information about the browser and the trailing slash after the hostname, see the following question on the Webmasters stack:

https://webmasters.stackexchange.com/questions/35643/is-trailing-slash-automagically-added-on-click-of-home-page-url-in-browser

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • Here is what my host said: Hi there, We do use nginx as a reverse proxy, however apache processes the .htaccess files and does all the mod rewriting so nginx really is not relevant to how apache handles the .htaccess files. – ridertiger Oct 31 '20 at 15:59
  • "so nginx really is not relevant to how apache handles the .htaccess files." - Except that the request from Apache must go back through the Nginx proxy server before being served to the client. Did you try the last example above that simply redirects to `https://www.example.com/` (your domain)? In fact, what happens if you try removing this redirect completely? The point is, there is no double slash when it leaves Apache (from the examples posted above) - so where is the additional slash coming from? (Nginx would seem to be the only thing in the way?) – MrWhite Oct 31 '20 at 18:34
  • Note that 301 redirects are cached persistently by the browser, so you will need to ensure any caches are cleared before testing. Preferably test with 302 (temporary) redirects to avoid caching issues. – MrWhite Oct 31 '20 at 19:01
  • Thanks for the help, just changed the code to RewriteCond %{ENV:HTTPS} !on RewriteRule ^ https://zeugmarket.com/ [L,R=302] Unfortunately, no dice. I am trying to remember there was something I used to write on the command prompt in my windows 10 machine to flash my PC or something, cant remember it for the life of me. – ridertiger Oct 31 '20 at 19:29
  • `RewriteCond %{ENV:HTTPS} !on RewriteRule ^ https://zeugmarket.com/ [L,R=302]` – ridertiger Oct 31 '20 at 19:45
  • Maybe you're thinking of `ipconfig /flushdns` (as admin)? But that just clears the DNS caches. If using Chrome, you can disable local caching in dev tools > Network tab and check "disable cache". (I think Firefox has similar.) – MrWhite Oct 31 '20 at 22:48
  • I'm assuming you don't have access to anything outside of your Apache/`.htaccess` file? To be thorough, do you have any other directives in your `.htaccess` file? – MrWhite Oct 31 '20 at 23:02
  • Yeah, i was thinking of ipconfig /flushdns as admin, thanks for that. Dont know why it is not helping this time around. Yea, I have tried chrome dev tools, empty cache and hard reload, not helpful. It wont even reflect the changes I made on the index.html lol. I briefly used cloudflare dns then I saw that htaccess was not working properly, too many redirects and page rules was not available to me, so I went back to my premium ifast.net servers dns and htaccess is working fine. It is just this double slashes, inconvenient but oh well. – ridertiger Nov 01 '20 at 10:15
  • i just copied and typed your code on htaccess. what other files do you mean other than my website? There is also something called .wellknown I am using an ftp program. I am also accessing cpanel – ridertiger Nov 01 '20 at 10:17
  • "not helping this time around" - ultimately I don't think this is a caching issue, since I see the same double slash. Although failure to see updates to `index.html` could well be. "cloudflare dns ... htaccess was not working" - Cloudflare is also a _proxy_. The use of `ENV:HTTPS` could have been an issue here; you would have probably needed to check the `X-Forwarded-Proto` header instead (but _page rules_ are preferable). "what other files do you mean other than my website?" - Apache/Nginx server config? `.wellknown` is probably used for SSL cert renewal. – MrWhite Nov 01 '20 at 13:33
0

Just change the rewrite rule

Instead of

RewriteRule ^(.*)$ https://www.zeugmarket.com%{REQUEST_URI} [L,R=301]

write

RewriteRule ^\/(.*)$ https://www.zeugmarket.com%{REQUEST_URI} [L,R=301]

and you will get one slash at the end.