I am trying to write a multi rewrite function in htacess.
change http into httpS
remove www
remove .html
It should handle all the cases:
http://www.example.com/about.html
should be rewritten as
http S://example.com/about
But of course if there is only one item to rewrite it should work too:
http://example.com/about
should be rewritten as
http S://example.com/about
Here is my code (https and no-WWW work but the no-.html which is commented does not work).
Please note that when rewriting you need to use the previously rewrited URL and not what the user typed into his address bar otherwise the rule will overwrite the change that another rule did.
RewriteEngine on
#strip the www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
#remove .html if any
#RewriteCond %{REQUEST_FILENAME}.html -f
#RewriteRule ^(([^/]*/)*[^/.]+)$ /$1.html [L]
#switch to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
# Disable the server signature
ServerSignature Off
You can use:
RewriteEngine on
#strip the www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
#switch to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
#remove .html if any
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
# Disable the server signature
ServerSignature Off
Related
I have installed an SSL on my website and I have a .htaccess file that has some conditions already written. I have looked at a number of methods shown online on how to force https. However, none of them seem to work. I keep having broken links.
The code I am currently using, the rule commented out does not work for https
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
#This condition doesn't seem to work
#RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect non www. to www.
#RewriteCond %{HTTP_HOST} !^www\.MYWEBSITE\.co\.uk
#RewriteRule (.*) http://www.MYWEBSITE.co.uk/$1 [R=301,L]
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1? [R=301,L]
# Remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php)
RewriteRule ^(.+)\.php /$1 [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
If you would like to force HTTPS you can use the following:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
How would i remove www from my url and force https and remove .html all at the same time?
RewriteEngine on
# force ssl
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
# Remove .html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
Try adding the following to your .htacces instead of your code above.
RewriteEngine on
#Force HTTPS.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Start rewrite.
RewriteBase /
# Load page without extension.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
# 301 While Rewrite.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
# Handle Trailing Slashes.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R,L]
# Rewrite rule to add .html extension back internally.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+[^/])$ $1.html
This will result in http://www.example.com/test.html to be handled by the browser as https://example.com/test while still loading the test.html file.
Put the following code at .htaccess in main directory
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://yoursite.com/$1 [L,R]
#the code above will redirect the entire site into https without www unless the request come with https://www .
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^ https://yoursite.com/$1 [L,R]
#The code above will catch any request with www if passed first code.
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.html[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
#the code above will remove any .HTML.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
# you can use the two lines if file without extension not work and you want to map them to .html files and if you want to go the same file .html without extension keep it as it is but , If you want to change it to same name .php file , replace $.html in the last line to $.php .
My htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^m/(.*)$ /$1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule products-([0-9]+).html$ products.php?page=$1 [NC]
RewriteRule services-([\-]?[0-9]+)-(.+).html$ services.php?id=$1 [NC]
RewriteRule objects-([\-]?[0-9]+)-(.+).html$ object.php?id=$1 [NC]
Wordpress Line
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I have an error.
if someone try domain .com/product-6-orange.html/something
he got a the same page as domain .com/product-6-orange.html
I dont want that domain .com/product-6-orange.html/something return a correct answer.
I want that
domain .com/product-6-orange.html/something redirect to domain .com/product-6-orange.html
so anything after .html need to be remove .
but how ??
I want that domain.com/product-6-orange.html/something redirect to domain .com/product-6-orange.html
Insert this redirect rule just below RewriteEngine on line:
RewriteRule ^(.+?\.html)/.*$ /$1 [NC,L,R=301]
Is there any way to have the first URL redirect to the second URL?
http://www.example.com/podcast/episode.html
http://www.example.com/podcast/episode
Is there any way to force the .html extension to redirect to the non-html version of the URL, so as not to show up as duplicate copies of the same page.
My htaccess code at the moment is:
RewriteEngine On
RewriteBase /
#removing trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
#non www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#shtml
AddType text/html .html
AddHandler server-parsed .html
#html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
#index redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://example.com/ [R=301,L]
Is this possible?
Not entirely sure if I understand the question, but if you want anything ending in .html to redirect to the same URL without the .html, this should do the trick:
RewriteRule ^/(.*)\.html$ /$1 [R=301,L]
This is how you can do the redirection from html to non-html only.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (/[^\ ]+)\.html\ [NC]
RewriteRule ^ %1 [L,R=301]
My URL structure is like
http://www.example.com/folder/index.php?dir=dir1
To be able to access it from
http://www.example.com/folder/dir1
and at the same time redirect the 1st URL to 2nd one, my htaccess (in 'folder') is
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301,NE]
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]
The Redirect and Rewrite were working perfect until recently I switched from NGINX back to APACHE.The trouble is that it is now also rewriting file extensions which I don't want!How can I only apply it only to directories and exclude ALL files from it?
Figured it out! :)
Actually had to add
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
before the first rewrite!
So the htaccess looks like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]