Struggling with .htaccess (Godaddy Linux Hosting) - linux

I would really appreciate your help with the below. I have been both googling and testing code via trial an error, I believe that I'm slowly figuring things out but I would appreciate the fast track assistance in both the result and my learning development.
I currently have a website hosted on a shared resource on a apache Linux server through GoDaddy. I am trying to achieve the following;-
1) Redirect non www traffic to www.
2) Remove the .html file extension and add a trailing slash on the web address.
I currently have the code below, the 301 www. redirect is working and the trailing slash is also working. However I am getting 404 errors on the subpages ie www.swiftcomm.co.uk/contact/;-
How do I go about resolving this issue with the code?
Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine on
# Redirect to domain with www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Ensure all directory URLs have a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Same for HTTPS:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]

Please try the following code - it can replace your entire file. The comments explain what each ruleset does.
Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine on
RewriteBase /
# Force www. prefix - http/https
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
# Remove .html extension suffix from URI only if it matches an existing file.
# Add trailing slash simultaneously.
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s\/(.+)\.html\s [NC]
RewriteRule ^ %1/ [R=302,L]
# Force trailing slash if it has'nt been forced already.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^\/(.+[^/])$
RewriteRule ^ %{REQUEST_URI}/ [R=302,L]
# Rewrite non-existing files to their .html counterparts.
# If the .html file does not exist, Apache will gracefully degrade to a 404.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1.html [L]

Related

How do I reduce redirect chain with .htaccess?

I've been having some problem with solving the redirect chain of my website. I wanted my website to ultimately have only one version, which is HTTPS + non-www + trailing slash
When tested, below htaccess gave me 4 response with redirect chain out of 8 total variation tested (https, www/non-www,trailing slash, 2x2x2 = 8 variations).
The http://www. variation gave me a redirect chain with 3 redirects
http://www. --> https://www. --> https://www. + trailing slash ---> https:// + trailing slash
Is there a way that I can make it no redirect chain or at least not more than 2?
Appreciate if someone could help me out! Thanks
# Force trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [L,R=301]
# HTTPS forced by SG-Optimizer
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
</IfModule>
# END HTTPS
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I use this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [L,R=301]
</IfModule>
If I understood you correctly, you want to force HTTPS//non-WWW and remove trailing slashes from files in one redirect. That's a bit demanding and require some sort of GOTO/SKIP logic in the .htaccess file. Try it like this with a fresh browser in incognito mode:
RewriteEngine on
## Check if not a directory and ends in /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
## If not a directory skip next RewriteRule
RewriteRule ^ - [S=2]
## Check if HTTPS and non-WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
## This RewriteRule skipped if URI was a directory
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
## This RewriteRule used if URI was a directory
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [S=1]
RewriteRule ^(.*)/$ https://example.com/$1 [R=301,L]
PS: I get the idea, but Google's Gary Illyes tweeted in 2016 "30x redirects don't lose PageRank anymore." So, even for SEO this is no longer a burning issue.

htaccess no-trailing-slash policy: non existing urls (404) should not redirect

If I open a non existing url on my site like www.domain.de/abcde/ (with trailing slash) the url redirects to www.domain.de/abcde (without trailing slash) and opens the 404 site after.
But the 404 site should come directly when the url does not exist (without redirecting for the non-trailing-slash policy).
What do I need to add to my htaccess? Thank you!
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
------
starkeens suggestion does only work on a clean htaccess, but I got some more code. It does not work together with the other lines. All the code:
RewriteEngine on
RewriteBase /
# redirect urls without www. to url with www.
RewriteCond %{HTTP_HOST} ^domain\.de$
RewriteRule ^(.*)$ http://www.domain.de/$1 [L,R=301]
# hide the suffix of urls
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+)\.html\ HTTP/
RewriteRule (.*).html$ /$1 [R=301,L]
# start redirect everything to the subfolder
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de [NC]
# except these folders:
RewriteCond %{REQUEST_URI} !^/downloads [NC]
RewriteCond %{REQUEST_URI} !^/cms [NC]
RewriteCond %{REQUEST_URI} !^/vorschau [NC]
RewriteCond %{REQUEST_URI} !^/domain/.*$
RewriteRule ^(.*)$ /subfolder/$1
# end redirect everything to the subfolder
# no-trailing-slash policy
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
Any new suggestions? :)
TRY :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ - [R=404,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
Finally i got a solution that seems to work.. maybe anyone got the same issue and needs the code. Comments for optimization are welcome!
# trailing Slashes
# to enforce a no-trailing-slash policy with subfolder
# is the request for a non-existent file?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# if so, skip the next RewriteRule
RewriteRule .? - [S=1]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
Now only existing files get the redirect of my no-trailing-slash policy. Non existing files aren't redirected and occur a 404 page directly, as wished.

.htaccess force www and force https for certain path and Concrete5 pretty URLs

edit: This question is now moot as the whole site will be served using HTTPS
My .htaccess is causing a redirect loop.
I need all three sections to work, the purpose of each is in the comment.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Force HTTPS for /book unless dev or already there
RewriteCond %{HTTP_HOST} !^dev\.
RewriteCond %{HTTPS} !^on$
RewriteRule ^book https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Force www prefix unless dev or already there
RewriteCond %{HTTP_HOST} !^dev\.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ %{REQUEST_PROTOCOL}://www.%{HTTP_HOST}/$1 [L,R=301]
# Concrete5 pretty URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
I'm having to work on the live server because the certificate is only valid there (with www. required).
I've tried many variations of the above, but am stumped, so am hoping fresh eyes on this will help, many thanks in advance.
Your 2nd rule isn't looking right as REQUEST_PROTOCOL has value of HTTP/1.1.
You can use this code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Force HTTPS for /book unless dev or already there
RewriteCond %{HTTP_HOST} !^dev\.
RewriteCond %{HTTPS} !^on$
RewriteRule ^book https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# Force www prefix unless dev or already there
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} !^(?:www|dev)\. [NC]
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
# Concrete5 pretty URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>

.htaccess 301 Redirect non slash domains and slash domain to .html

I am trying to add some code to my .htaccess to redirect slash and non slash urls to the .html all url's apart from my homepage.
For example
www.mydomain.com/cat/ and www.mydomain.com/cat
should redirect to www.mydomain.com/cat.html
I have managed to add the following to my .htaccess which redirects www.mydomain.com/cat to the right place www.mydomain.com/cat.html but need some help on how to make slash version redirect to the .html page
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
My whole .htaccess looks like this, if anyone has any suggestions on how it should look in light of the above it would be greatly appreciated.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^xxx.xxx.xxx.xx [nc,or]
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
RewriteEngine on
# Pleas note that RewriteBase setting is obsolete use it only in case you experience some problems with SEO addon.
# Some hostings require RewriteBase to be uncommented
# Example:
# Your store url is http://www.yourcompany.com/store/cart
# So "RewriteBase" should be:
# RewriteBase /store/cart
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]
</IfModule>
SOLVED:
I just added:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
The separate rules seems to be working for you, but I think you can simplify it to one rule, with an optional slash. Your rule redirects the slash to no-slash, which then redirects again to the .html. With one rule, you'd only have one redirect.
This has the standard RewriteCond that check if it's not a file or a folder, so it doesn't keep redirecting .html if it's already one. Then, the \? in the ReweriteRule is an optional slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ http://mydomain.com/$1.html [R=301,L]
If this is all in your domain, you can omit it from the result:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ /$1.html [R=301,L]
Also, note this will catch and work with subfolders, whether or not you mean it to. e.g.,
www.mydomain.com/animals/cat/ will redirect to www.mydomain.com/animals/cat.html

Excluding specific pages from HTTPS in mod_rewrite

I am trying to setup a codeigniter app to force HTTPS across all pages except one. However, I cannot get the rules to only redirect if the user is not on the page in question.
The page that should be excluded has the following URL's:
http://mydomain.com/kpi/reports/67
http://mydomain.com/kpi/reports/67/overview/2013-02-01/2013-02-28
http://mydomain.com/index.php?/kpi/reports/67
http://mydomain.com/index.php?/kpi/reports/67/overview/2013-02-01/2013-02-28
The number 67 and the dates can all change in the URL's above hence the user of regular expressions below.
I have tested the regular expressions and they seem to match the URL's fine. However, the htaccess just seems to redirect it to https:// anyway.
My .htaccess file is as follows...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Disallow access to system dir
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Disallow access to application dir
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Force https when not on overview report
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/index\.php\?/kpi/reports/?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/index\.php\?/kpi/reports/?([0-9]+)/overview/?([0-9]+)-?([0-9]+)-?([0-9]+)/?([0-9]+)-?([0-9]+)-?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/kpi/reports/?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/kpi/reports/?([0-9]+)/overview/?([0-9]+)-?([0-9]+)-?([0-9]+)/?([0-9]+)-?([0-9]+)-?([0-9]+)$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,L]
#If not a valid file, redirect request through index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Any help would be greatly appreciated!
Maybe this will do what you need:
#Force https when not on overview report
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} !kpi/reports/[0-9]+/?$ [NC]
RewriteCond %{QUERY_STRING} !kpi/reports/[0-9]+/overview/[^/]+/[^/]+/? [NC]
RewriteCond %{REQUEST_URI} !kpi/reports/[0-9]+/?$ [NC]
RewriteCond %{REQUEST_URI} !kpi/reports/[0-9]+/overview/[^/]+/[^/]+/? [NC]
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,L]
#If not a valid file, redirect request through index.php
Replace all lines between the comments.

Resources