-1

So let us say that my website is www.example.com and I have a contact.html page in the root. The URL normally shown in the address bar is: http://example.com/contact.html

For my site, I want the URL to have:

  1. no www
  2. no .html
  3. a trailing slash

So the final result should look like this: http://example.com/contact/

Condition No. 1 is default, so that doesn't bother me.

For Condition 2 (no '.html'), I have the following code into my .htaccess file place in the root directory:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

...and it works perfectly.

Now coming on to Condition 3 (enforcing trailing slash), I have the following code into the .htaccess file before the 'Condition 2' rewrite commands:

RewriteEngine On
RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]

...this does not work; The page redirects to the Error 404 page and the address bar shows: http:/example.com/404.html... YES, with the trailing slash :/

What am I doing wrong?

I have a folder called archives in the root directory of which I want indexing to be done. So I simply added a .htaccess file in the 'archives' folder and added the following, simple code:

Options +Indexing

But it doesn't work; I get redirected to the Error 403 page! I'm 99% sure that it is due the .htaccess file in the root directory.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mihir Chaturvedi
  • 266
  • 1
  • 5
  • 12
  • 1
    Please excuse me not answering. When I read a question containing `.htaccess` and "weird", I automatically scan for `RewriteRule` in the question's body. And lo, I _always_ find one. Your question has the unfortunate bad luck to also feature the term "copy-pasted", which really discourages me to craft an answer. I am sorry about that lack of motivation on my part! Perhaps this page will help you further: https://httpd.apache.org/docs/2.2/rewrite/ – Boldewyn Nov 04 '16 at 13:54
  • Oh, I forgot your additional problem. Sorry about that! The answer is: "yes, the `.htaccess` in the page root is causing that issue." – Boldewyn Nov 04 '16 at 13:56
  • I think you are facing this issue : http://stackoverflow.com/questions/40412585/htaccess-remove-extension-but-allow-404-if-not-existing – Amit Verma Nov 04 '16 at 14:00
  • @Boldewyn I used 'copy-pasted' only because I really did, but wisely, of course. Also, what's the solution to the second problem? – Mihir Chaturvedi Nov 04 '16 at 14:05
  • @starkeen Edited the question – Mihir Chaturvedi Nov 04 '16 at 14:06
  • See the linked question. – Amit Verma Nov 04 '16 at 14:07
  • Let the `.htaccess` in the root not unconditionally take over requests for missing files (the `!-f` part). E.g., add something like `RewriteCond %{REQUEST_URI} !/archives/.*` or similar. – Boldewyn Nov 04 '16 at 14:47

1 Answers1

1

Try with:

RewriteEngine On

RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule (.+) %{REQUEST_URI}/ [R=301,L]

# remove html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.html -f 
RewriteRule ^([^\.]+)/?$ $1.html [NC,L]
Croises
  • 18,570
  • 4
  • 30
  • 47