.htaccess redirect issue back to homepage - .htaccess

I have a smallr edirect issue with a clients website! Now I want all of the links to redirect to the ssl and www version of the website. This is what I have on the htaccess file now...
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule .* https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.example.com/$1 [L,R=301]
</ifModule>
the problem arises when a non-https link is clicked to a specific page.
for example if someone clicks http://www.example.com/somepage it automatically gets redirected to the actual homepage of the site and not the specific page itself!
am I missing something here?
Many thanks.

Related

www htaccess redirect with segment in url

Where my site is hosted, I'm using .htaccess and it has a condition to remove the www and direct to the main page without the www.
<ifModule mod_rewrite.c>
Header set X-Frame-Options DENY
RewriteEngine On
# Required to allow direct-linking of pages so they can be processed by Angular
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://meusite.com.br [R=301,L]
</ifModule>
The problem is this, when someone accesses an internal page with www, it falls for this check and is directed to the home, example:
If someone accesses the link: http://www.meusite.com.br/conteudo/94-vai-criar-um-site-to-your-employee-said-you-can-noble
It will direct to http://meusite.com under the condition.
What I need, is that it is directed to the following link: http://meusite.com/content/94-vai-create-a-site-to-your-employee-behavior-which-cannot-can- -fine only by removing the www from the link.
Does anyone know if this check is possible in .htaccess?
EDIT:
.htaccess is not able to translate your titles from portugese to english.
You should do redirection to normal domain with full link, and then do internal redirection with your backend (i.e. php, ruby) to proper translated link.
Use following code before your redirection, so links with conteudo will be catched here and redirected properly using backreferences:
RewriteCond %{HTTP_HOST} ^www\..* [NC]
RewriteRule ^\/conteudo\/(.*)$ http://menusite.com.br/content/$1 [R=301,L]
Soluction:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Redirect domain to WWW

I want to make a URL change whenever clients access my website with example.com to wwww.example.com I have used .htaccess for this but it works not correct.
Because now a-z0-9.example.com wil also redirect to www.example.com, I want this only works for example.com than redirect to www.example.com
.HTACCESS
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.eample.com/$1 [L,R=301,NC]
Make another .htaccess for a-z0-9.example.com with the code bellow:
RewriteEngine on
RewriteCond %{HTTP_HOST} a-z0-9.example.com [NC]
RewriteRule ^(.*)$ http://www.a-z0-9.example.com/$1 [L,R=301,NC]
I think this will work.
Good Luck!

.htaccess rule to remove index.php from the end of my urls

I have seen this asked before without an answer here: .htaccess rule to remove index.php at the end of urls
I'm having the same issue as the original poster. I have urls on my Joomla site that are showing up with index.php at the end. I want all urls ending with index.php to be redirected to the same page without the index.php at the end.
Example: www.mysite.com/forum/index.php should be redirected to www.mysite.com/forum
I have tried this code
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
But it redirects all url's with index.php at the end to my sites home page
Example www.mysite.com/forum/index.php ends up being redirected to www.mysite.com
Any help on this would be greatly appreciated!
You can use this rule as your very first rule:
RewriteCond %{THE_REQUEST} !/admin/ [NC]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]

Infinite loop error with .htaccess when I add 301 redirect with WordPress in subdirectory

I have my WordPress website in a subdirectory. I used the WordPress codex directions to set this up and all worked well until client asked me to add a 301 redirect to solve an issue with some links to an old domain on the Internet. The old domain name points to the new website but does not use a permanent 301 redirect. The current domain name is Encoreco.com.
The old domain points to the same place but it is not a 301 redirect (you can see that the URL does not change at the top and for some reason this means that my javascript slideshow doesn't work). Here is what happens: encoreconstructionco.com
When I add a 301 redirect to solve this problem, I get the infinite loop errors. Here is the line of code that attempts to solve the issue with the old domain but creates the infinite loop:
RewriteRule (.*) http://encoreco.com/$1 [R=301,L]
And here is my current .htaccess: Note - I tried taking the .html rewrite and the www rewrite out and that did not solve the problem.
RewriteCond %{THE_REQUEST} \.html
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://%1/$1 [R=301,L]
# 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
RewriteRule (.*) http://encoreco.com/$1 [R=301,L]
… will redirect everything, including correct requests, so you have to get an infinite redirect.
Restrict that rule to wrong host names only:
RewriteCond %{HTTP_HOST} !encoreco\.com
RewriteRule (.*) http://encoreco.com/$1 [R=301,L]

Redirecting mysite.com/somepage/thispage.php to www.mysite.com/somepage/thispage.php

I tried doing this
RewriteEngine On
rewritecond %{http_host} ^mysite.com
rewriteRule ^(.*) http://www.mysite.com/$1 [R=301,L]
but when i go to mysite.com, it took me to http://www.mysite.com/mysite. So I removed the $1 and that seemed to fix the issue.
But now that when I go to mysite.com/somepage/thispage.php, it does not work and shows the page not found (the www.mysite.com actually points to where the site is hosted, thats why I want mysite.com to go to www.mysite.com). How can I redirect that to mysite.com/somepage/thispage.php and also the other anything appended to mysite.com?
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Redirects all non-www url's to www.

Resources