Redirect .html to / - .htaccess

We are looking for a solution to redirect our .html pages to pages with a / so for example:
old:
www.domain.com/url.html
New:
www.domain.com/url/
We found the following method to delete the .html, this only does not place a / on the end:
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
So with the following method you delete the .html but there is not a / placed on the end. Is there a way to replace the .html with a /?
Hope someone can help

Try below rule,
RewriteEngine On
#redirecting to extension less url with forward slash
RewriteRule ^(.*)\.html$ $1/ [R=301,L]
#handling the extension less url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1.html [L]

Add this to the pages you want to redirect:
<meta http-equiv="refresh" content="0; url=http://example.com/" />

Related

removing index.php and .php extension from all pages using .htaccess

I had added this code in .htaccess for removing .php extension from the url's.Also I had written a code for making blog url seofriendly like https://url.com/posttitle
<IfModule mod_rewrite.c>
#removing extension from url
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#blog
#if the file with the specified name in the browser doesn’t exist, or the directory in the browser doesn’t exist then procede to the rewrite rule below
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ blog_detail.php?id=$1 [QSA,L]
</IfModule>
I had added links like Home.
Home
How to remove index from url in this case?
You can simply use a "/" in your href attribute when navigating to your root (index.html/.php)

How do I hide a /public/ directory in htacess

My website is website.com/public/login.php. That looks a bit ugly. How do you rewrite htaccess to say website.com/login.php?
I've tried
RewriteEngine On
RewriteBase /public/
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
The RewriteRule that strips off the /public/ path is currently an external redirect, because of the R=. Since you want the result of that rule to be hidden, it should rather be an internal redirect (also called URL forwarding), and that is written without the R=302 option.
While you're at it, you could also hide the .php from the externally visible URLs since a simple /login looks much cleaner than /login.php.
Solved. It hides /public/
website.com/login.php instead of
website.com/public/login.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>

.htaccess Rewrite to Force Trailing Slash after remove .html

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!
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!)
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

Remove .php extension from url

I am required to remove .php extension and query string as well from url by rewriting the url
I know this can be done in .htaccess file
I have this in my .htaccess file so far
RewriteEngine on
DirectoryIndex Home.html
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.html
RewriteRule .* Home.html
now the url
example.com/blog.php?post=12&comment=3
can be accessed by
example.com/blog.php/post/12/comment/3
but i want to remove .php extension as well
this must be accessible through this url
example.com/blog/post/13/comment/3
Any help?
Thank you in advance.
Searches for everything except . after /
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

redirecting files with html extension to files without extension (in url)

I have recently changed my website url using htaccess so that my urls will not show file extensions. Now my problem is as I have created a new xml sitemap so that my url will be extensionless!!! the Google webmaster tool is telling me about duplicate content issue!! ie. page and page.html have same title.... so my question is how do i redirect the urls with file extension html to urls with out extension!!!
this is an example of my website url with html extension
http://www.shenazhpeyk.co.uk/coding-machines.html
I want to redirect and change it to
http://www.shenazhpeyk.co.uk/coding-machines
so that will fix the issue with Google webmaster tools (Please provide me a code for use in htaccess file)
Many Thanks
Found this code as well. Not sure if it will accomplish the same thing. Seems to work for me as does the one above (for PHP).
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
I am wondering about the trailing slashes and whether those should be there or omitted?
Try adding the following to the .htaccess file in the root directory of your site redirect URLs with .html extension and remove it.
RewriteEngine on
RewriteBase /
#redirect to remove the .html extension
RewriteRule ^(.+)\.html$ $1 [L,NC,R=301]
Try this:
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]

Resources