Strange behavior of the htaccess redirection with www and slash - html

I make an analyse of the site and was faced with a problem in htaccess redirection. It is a strange, I did it many times before and had never the problem like this.
So, I try to just redirect my domen to without www and shash:
ErrorDocument 404 /404.html
RewriteEngine On
RewriteBase /
rewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://domen.com/$1 [R=301,L]
And also I remove the slash at the end of url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
And after this I make a redirect from non-existing or unused page to 404 error:
RedirectMatch 404 ^/non-existing-page$ or ^/non-existing-page/$
When I type in the browser http://www.domen.com/non-existing-page (with www) or http:domen.com/non-existing-page (with slash) a have the same url with 404 error code and 404 html code. It's all right.
But if I write the url http://www.domen.com/non-existing-pag/ (with www and shash) I'm redirected to 404.html page with the 200 - no error code.
What's wrong?
At the start of .htaccess I have only
<IfModule mod_speling.c>
CheckSpelling on
</IfModule>
That's all.

Related

Rename urls using htaccess in BlueHost

I want to change/rename urls in my website. For now I'm just removing .html in url like this:
ErrorDocument 404 /errorpage.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
I want to change urls. For example: index.html -> home, aboutus.html -> about-us etc. I tried changing but I got server client error 500:
ErrorDocument 404 /errorpage.html
RewriteEngine on
RewriteRule ^index\.html /home [NC,L]
RewriteRule ^aboutus\.html /about-us [NC,L]
RewriteRule ^index\.html /home [NC,L]
RewriteRule ^aboutus\.html /about-us [NC,L]
It's not clear why you would get a 500 error here, unless you had combined these rules with the former rule that handled .html extensionless URLs (this would have caused a rewrite-loop, ie. 500 error).
However, the arguments you are passing to the RewriteRule directives are the wrong way round. You should be rewriting from /home to /index.html instead. The first argument matches the requested URL. The second argument (the substitution string) represents the URL (or file-path) you are rewriting to.
Your directives should be like this instead:
ErrorDocument 404 /errorpage.html
RewriteEngine on
RewriteRule ^home$ index.html [L]
RewriteRule ^about-us$ aboutus.html [L]
The slash prefix on the substitution string (second argument) is not required.
You should be linking to URLs of the form /home and /about-us in your HTML source and mod_rewrite internally rewrites these requests to the corresponding file that actually handles the request.

The page isn't redirecting properly error http - https auto redirect showing error in website

Whenever i put the below http - https auto redirect code i get this error "The page isn't redirecting properly
An error occurred during a connection to "Website URL".
This problem can sometimes be caused by disabling or refusing to accept cookies."
The htaccess code is below.
# Rewrite engine code https redirect
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Rewrite engine code for www to non ww version of website
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
You must add a test, because at the moment you run the first rule for each request.
And redirect without www before https only.
Try with:
RewriteEngine on
# http(s)://(www.)?domain -> https://domain
# www -> https without www
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
# http -> https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

htaccess why is site redirecting to //

In my htaccess I am redirecting all non https to https.
It works, but its also adding in an extra '/', so the url is
'https: //www.[MY SITE URL].co.uk//'
Why is this? To be honest, I don't really know what all this in my htaccess is doing, its copied from googling answers to 'redirect all requests to https'
My htaccess:
Options -MultiViews
RewriteEngine On # Turn on the rewriting engine
RewriteBase /
#RewriteCond %{HTTP_HOST} ^(www\.)?jobooz\.com [NC]
#RewriteRule ^(.*)$ https://www.jobooz.co.uk/$1 [R=301,L,NC]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/$1
RewriteRule ^/?$ /php/index.php [NC,L] # Home page
I've also noticed any deeper urls like '/search-jobs/jobs-near-me' that I add to the url then get duplicated too, to '/search-jobs/jobs-near-me/search-jobs/jobs-near-me' when redirected to https.
All urls work fine if I go directly to the https version.
Any help appreciated, thanks.
The following rewrite rule has a problem:
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]
The gist of why you are seeing repeated fragments in the rewritten URL is that (.*) matches everything, and that already includes the host and URI. Instead, you can try redirecting any incoming request on port 80 to HTTPS.
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

www to non-www .htaccess not working

EDIT: The problem was that the domain was purchased at GoDaddy, but hosted at Endurance Internation Group (they own Hostgator and a few other small web hosts). Endurance doesn't have mod_rewrite activated by default, which is why this wasn't working.
I have hosted the site on GoDaddy and now everything works 100%.
I have a website that Google is indexing the www and non-www version of. I only want the non-www version. This is my current .htaccess code. The 301 redirects of individual files at the bottom work, but the www to non-www code at the top does not work.
What am I doing wrong?
RewriteEngine On
RewriteBase /
RewriteCond % ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
# Redirect old file path to new file path
Redirect 301 /aboutcarytreeservice.html http://example.com/about.html
Redirect 301 /carytreeservice.html http://example.com/services.html
Redirect 301 /treeremovaldurhamnc.html http://example.com/durham.html
Redirect 301 /treeservicedurhamnc.html http://example.com/durham.html
Redirect 301 /treeremovalraleighnc.html http://example.com/
This condition is wrong:
RewriteCond % ^www.example.com [NC]
change it to:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
It will look to see if there is a "www" in the URL and cause a 301 redirect to the same URL (with "www" added) if it is not.

.htaccess conditional redirect https to http

I'm trying to make all https requested to my site route to http EXCEPT for anything at /shop
Using the following code in my .htaccess but it's not working:
#redirect all https traffic to http, unless it is pointed at /shop
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/shop/?.*$
RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]
Hosted on rackspace cloud sites.
Other code in .htaccess
RewriteEngine on
RewriteBase /
# Shop rewrite rule + remove trailing slash
RewriteRule ^shop\/(.*)/$ shop/$1 [R=301,L]
RewriteRule ^shop\/([A-Za-z0-9-/]+)$ shop.php?shop=$1 [NC,L,QSA]
DirectoryIndex index.php index.html index.htm