There is a problem with my .htaccess file. If I type in "website.com" it redirects me correctly to "www.website.com", but if I type in "website.com/level1/level2" it redirects me to "www.website.com/index.php/level2" and gives me a 404 error.
Here is what I have in my .htacces file:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
AddDefaultCharset UTF-8
#Redirect from website.com to www.website.com
RewriteCond %{HTTP_HOST} ^website\.com$ [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]
Any suggestion how to solve the issue?
Thank you.
I think what's happening is that you are rewriting to index.php before you are doing the external redirect. If you switch the order of your RewriteRules, it should fix the problem. Try this:
RewriteEngine on
#Redirect from website.com to www.website.com
RewriteCond %{HTTP_HOST} ^website\.com$ [NC]
RewriteRule ^(.*)$ http://website.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
AddDefaultCharset UTF-8
This way, the external redirect to www is done first, and when the request comes back, the internal index.php is then applied, instead of the other way around.
If you have access to the server config, it is better to do hostname redirects with plain old Redirect which is designed for this exact situation. Don't resort to the complexity of mod_rewrite until you really need to.
Remove the ServerAlias for example.com in the <VirtualHost> for your main web site, and add it as a separate virtualhost instead:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
Always put rules that cause an external redirect before those rules that just cause an internal redirect. Otherwise an already internally rewritten URL would be redirected externally using the internal URL.
So in your case swap the order of your two rules putting the rule with the R flag before the other rule:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
Related
I have two domains pointing to the same place but I only want one of these to redirect all http requests to https.
http://www.thisisme.com should stay as it is
http://thisisme.mydomain.com needs to redirect to https://thisisme.mydomain.com
As an extra rule I need to make sure that http://thisisme.mydomain.com/health_check.php does not redirect.
Here's what I have so far.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Don't redirect /heath_check.php to https
RewriteCond %{REQUEST_URI} !(health_check\.php)$
#This line needs looking at! Should only redirect any xxx.mydomain.com requests to https
RewriteCond %{HTTP_HOST} mydomain\.com
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# block hidden directories
RewriteRule "(^|/)\." - [F]
# This makes the url look pretty for codeigniter...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
</IfModule>
EDIT!
I actually need to do the opposite if the requested domain is not mydomain.com - it needs to redirect to http is https is requested
To exclude www.thisisme.com ,put the following condition right bellow RewriteEngine on line or above the http=>https rule :
RewriteCond %{HTTP_HOST} !^(www\.)?thisisme\.com$ [NC]
I have added a force redirect from non-www to www on my codeigniter based web app.
After doing that it is working fine, but its showing an extra index.php in URL which I need to remove.
Here is my URL : http://www.testprofile.com (WORKING FINE)
Here is problem caused :
https://testprofile.com/result/184/1103511096450940jawadamin (non www site, it will automatically add www.)
opening the above URL redirects to www but also adds an additional index.php in URL which should be removed.
HERE IS THE CODE OF MY .HTACCESS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTP_HOST} ^testprofile.com [NC]
RewriteRule ^(.*)$ https://www.testprofile.com/$1 [L,R=301]
</IfModule>
Please help me remove this annoying index.php
Thanks,
You missed to escape dot in right hand side of rewrite condition line. It should be like
RewriteCond %{HTTP_HOST} ^testprofile\.com [NC]
or in your whole example
<IfModule mod_rewrite.c>
RewriteEngine On
#If in root of web server you can use next line or set it appropriate according to location on server
#RewriteBase /
RewriteCond %{HTTP_HOST} ^testprofile\.com [NC]
RewriteRule ^(.*)$ https://www.testprofile.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
If dot is not preserved by back slash character apache read it as wild card character. You can find some useful tips on this page.
Also, you can check useful .htaccess snippets here.
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]
This is driving me bananas. For some reason my domain will not work to redirect https-WWW to https-non-WWW. This works with every other permutation except https://www.nextoronto.com
What code do I use in the .htaccess that will allow it to work for all permutations?
Edit: This is the rewrite code now:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.nextoronto\.com [NC]
RewriteRule ^(.*)$ https://nextoronto.com/$1 [L,R=301]
It looks sound, perhaps that rule isn't being reached due to other rules in front of it?
Here is what I use that works:
# Force SSL
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://nextoronto.com/$1 [R=301,L]
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://nextoronto.com/$1 [R=301,L]
# My other rewrites for routing all requests to index.php go here...
Edit the URL and try with this code:
RewriteEngine on
rewritecond %{http_host} ^www\.example\.com [nc]
rewriteCond %{SERVER_PORT} 80
rewriterule ^(.*)$ https://example.com/$1 [r=301,nc]
Step 1
You should make .htaccess file like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1
Step 2
Go to the directory, then view mark the option file extension. Then see the .htaccess file.
When no rewrite works, no matter what you do, consider what happened to me. IIS or WAMP, on my laptop, redirected all calls to my domain (call it example.com) to localhost. So creating all the rewrites on my example.com server were to no avail because no calls to example.com ever made it out of my computer. Just do a "ping example.com" and if you get an IP of 127.0.0.1, edit your HOSTS file.
Below is the code in my .htaccess file. How do I change it to redirect visitors to https://www and not http://.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Immediately after the RewriteEngine On directive, place the following:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !www\.
RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,L]
This will handle both the http to https and non-www to www redirection. Note that external redirections should generally come before internal rewrites (which is what you currently have).