0

I understand somewhat similar questions have been asked and yes I did and read all the relevant answers and tried it all for quite some time. However I can't figure out what the error is. I believe the script is correct. What is going on?

My problem is that I can't make the trailing slash appear in the url. Therefore I get 2 versions of the same page. (Duplicate content issue?) For example:

htttps://www.example.com/faq.php

&

htttps://www.example.com/faq.php/

I had hoped to always automatically add the trailing slash so there is no other version of the same.

Here is my .htaccess code:

 <IfModule mod_rewrite.c>

    RewriteEngine On

    #Redirect anything to http://www (WORKING FINE)
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

    #Redirect anything to https (WORKING FINE)
    RewriteCond %{HTTPS} !on
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    #Redirect to trailing slash (NOT WORKING!)
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_URI} !(.*)/$  
    RewriteRule ^(.*)$ https://example.com/$1/ [L,R=301]

    #Redirect /index.php to / (WORKING FINE)
    RewriteCond %{THE_REQUEST} ^.*/index\.php 
    RewriteRule ^(.*)index.php$ /$1 [R=301,L] 

</IfModule>

Also one more question on the same.

How is it that when I go to my domain where the trailing slash "/" is not showing in the url. Then I copy & paste it into a text editor it comes up as https://www.example.com/ with the trailing slash.

And yet the same does not happen when I copy/paste an url from any subpage as above mentioned for example /faq.php (NOT shwoing as: /faq.php/).

I know that the trailing slash in the root domain is necessary to make a HTTP request valid but why aren't the browsers showing it? Just an aesthetic thing? Anyway I am more concerned about the above question.

Thanks a lot for the help!

1 Answers1

0

However I can't figure out what the error is. I believe the script is correct. What is going on?

You are checking if the file does not exist before redirecting. This is not what you want, so remove this line:

RewriteCond %{REQUEST_FILENAME} !-f 

How is it that when I go to my domain where the trailing slash "/" is not showing in the url. Then I copy & paste it into a text editor it comes up as https://www.example.com/ with the trailing slash.

Some browsers do this automatically. It is probably done because that is the actual URL requested. It also makes it look more like a URL.

Anonymous
  • 11,748
  • 6
  • 35
  • 57
  • Thanks a million for your answer. Appreciate it! For some strange reason it doesn't work. I am only using: RewriteBase / RewriteCond %{REQUEST_URI} !(.*)/$ RewriteRule ^(.*)$ https://example.com/$1/ [L,R=301] The result I now get is: "www.example.com redirected you too many times." ERR_TOO_MANY_REDIRECTS And the site not showing at all. – spotlightevents.ie Mar 23 '17 at 23:16
  • You changed the last line from your question. It should be `RewriteRule ^(.*)$ https://example.com/$1/ [L,R=301]`. – Anonymous Mar 25 '17 at 15:12