1

I have the following code in my htaccess file:

Options -Indexes
RewriteEngine On

# Redirect external .php requests to extension less url
RewriteCond %{THE_REQUEST} ^(.+)\.html([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.html$ http://%{HTTP_HOST}/foo/$1 [R=301,L]

# Resolve .html file for extension less html urls
RewriteRule ^([^/.]+)$ $1.html [L]

That work fine when I go to www.mydomain.com/foo/index whithout .html. I want force trailing Slash at the end of index but i don't know how

Does anyone know how to modify my code to make the trailing slash work ?

Thanks!

Laurent
  • 91
  • 10
  • Possible duplicate of [.htaccess Rewrite to Force Trailing Slash at the end](http://stackoverflow.com/questions/7780859/htaccess-rewrite-to-force-trailing-slash-at-the-end) – mani Jan 24 '16 at 00:12

2 Answers2

1

To add a trailing slash to your extension less html urls, You need to modify your both of the rules.

In the first rule, we add a trailing at the end of the target url so that a slash is added to all .html requests.

and in the second rule we add a trailing slash at the end of the regex pattern to accept a request with a trailing slash.

Options -Indexes
RewriteEngine On

# Redirect external .php requests to extension less url
RewriteCond %{THE_REQUEST} ^(.+)\.html([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.html$ http://%{HTTP_HOST}/foo/$1/ [R=301,L]

# Resolve .html file for extension less html urls
RewriteRule ^([^/.]+)/?$ $1.html [L]

Clear your browser's before testing this! (Hope,this helps!)

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
1

I modified my code for this:

Options -Indexes
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.html(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.html$ http://%{HTTP_HOST}/fr/$1 [R=301,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$ 
RewriteRule (.*)$ http://%{HTTP_HOST}/fr/$1/ [R=301,L]

It works well

Laurent
  • 91
  • 10
  • Now I would hide in the url the fr folder. I would like your help because I did not find anything conclusive on the subject. Thanks thanks ! – Laurent Jan 24 '16 at 17:09