Only the homepage redirects to www version and not the internal pages. Why? - html

I am trying to redirect all the requests to https www version of the website. Homepage redirects perfectly fine but any other page of the website doesn't. I checked the redirect rules mentioned in the htaccess and they work perfectly fine. However, on the live website, it doesnt.
Here's the redirect rules
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
Any reason why this is happening?
I tried working through different redirect rules to see which one redirects as per the requirement.

try this
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.yourSiteName.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.yourSiteName.com/$1 [L,R=301]

Related

Website uses URLs with www and non-www subdomain which may cause duplicate content and bad links

How can I redirect www to non www for sub-directories? It is OK for home page but for sub-directories, it doesn't work. My sub-directory files have dynamic content.
My .htaccess is:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteRule blog/(.*)/ viewpost.php?page=$1
RewriteRule blog/(.*) catpost.php?page=$1
but it only affects the home page, not sub-directories.

How to 301 redirect from map

I made a website where I made an English version as well. The English version map is in /en.
The normal site of mine, redirects with this code:
RewriteCond %{HTTP_HOST} ^www.mysite.nl$ [NC]
RewriteRule ^(.*)$ http://mysite.nl/$1 [R=301,L]
The English version is in the /en map so I thought this would work, but it doesn't.
RewriteCond %{HTTP_HOST} ^www.mysite.nl/en$ [NC]
RewriteRule ^(.*)$ http://mysite.nl/en/$1 [R=301,L]
Does someone know how to 301 redirect when the site is in a folder?
With your shown samples, please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules for putting en in uris.
RewriteCond %{HTTP_HOST} ^www\.mysite\.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/en [NC]
RewriteRule ^(.*)/?$ http://mysite.nl/en/$1 [R=301,L]
##Other rules for redirection.
RewriteCond %{HTTP_HOST} ^www\.mysite\.nl$ [NC]
RewriteRule ^(.*)/?$ http://mysite.nl/$1 [R=301,L]

Website links won't work with .htaccess code which removes .html extension

I have the following code in my .htacess file for my website to remove '.html' from my urls and to redirect any .html to the same page without it (example.com/home.html to example.com/home)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
However, when I try to click on links that are pointed to other pages on my website, I get a 404 error.
I've tried setting the href to http://www.example.com/page2, /page2 and more but I still get the same error. Any help?
Please save your code and put only the following code in your main directory .htaccess file :
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(.*?)\.html[\s?/] [NC]
RewriteRule (.*)$ /%1%2 [R=302,L,NE]
Note: clear your browser cache because you did 301 redirect and test the code

htaccess to redirect main domain

I have a htaccess that redirects my main domain "www.main.com" to "www.main.com/DBS2010".
The htaccess works but my url changes to "www.main.com/DBS2010" can I make the url stay as "www.main.com"?
htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?main.com$
RewriteRule ^(/)?$ DBS2010 [L]
You can try this code for that:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?main\.com$ [NC]
RewriteRule ^$ DBS2010 [L]
This will not change URL in browser to `/DBS2010'.

.htaccess redirect something to something.html

How to derirect
http://www.mysite.com/something
to
http://www.mysite.com/something.html
And
http://www.mysite.com/#something
to
http://www.mysite.com/something.html
Thanks.
For the first you can use,
RewriteEngine On
RewriteRule ^something((/)?)$ something.html [nc]
Or more generally if you want http://mysite.com/X to go to X.html,
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule ^([^.]+)((/)?)$ $1.html [nc]
http://www.mysite.com/#something can't be redirected with a rewrite rule, everything after the # never gets passed to the server. You will need to use JavaScript to redirect those links.