htaccess problems with redirect for no www - apache

I've been trying everything to manage a redirect from www.domain.com to domain.com,
but nothing seems to work for me. I always get a redirect loop - and I've tried various things I found here or on Google.
So here is my .htaccess, maybe someone could help me figure out what I can do to redirect correctly or if there is something wrong in here.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
# Redirect all to .php
# Example: example.com/hello -> example.com/hello.php
RewriteRule ^(.*)$ $1.php [L,R=301]
# show example.com/index.php always as example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]
Thank you so much!
I've already spent so much time trying to figure this out.

You have a rule that always matches, which is responsible for the infinite redirection. I've updated your ruleset below to fix that problem and perform the redirection you mentioned at the top of the answer. Let me know if this does what you expect.
RewriteEngine On
# Redirect www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^.*$ http://example.com/$0 [R=301,L]
# This performs an external redirection? Is that what you want?
# Don't do the rewrite if we're already pointing at a file, otherwise we'll
# just redirect over and over because .* matches what we redirect to, too
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^.+$ $0.php [L,R=301]
# show example.com/index.php always as example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]

The answer is Apache documentation, the documentation tell how to force usage of www. You just have to reverse the example.
RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://example.com/$1 [L,R]

Related

mod_write htaccess with subdomains

I've been trying to figure out this htaccess issue and I can't seem to get it working and getting stuck on 500 errors.
I've looked around at multiple S.O. questions and general google but haven't found any that apply to my particular situation. Here it is:
I have example.com. I want if the user puts in: example.com or www.example.com to go to index.php.
But if the user puts in a subdomain subdomain.example.com; I want it to go to
dashboard.php?val=subdomain
Further more I'd like
subdomain.example.com/contact
to go to
dashboard.php?val=subdomain&page=contact
Here is what I have so far:
RewriteEngine On
RewriteBase /
# If doesn't start with www and it has a subdomain, redirect
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule .? - [E=VAL:%1]
RewriteRule ^/?$ /dashboard.php?val=%{ENV:VAL}&page=idx
RewriteRule ^contact/?$ /dashboard.php?val=%{ENV:VAL}&page=contact [L]
#Note: I have tried this with and without the Env variables and only having 1 RewriteRule
# The "catch all other" redirect for images, files and if they do /cont by accident
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !dashboard\.php [NC]
RewriteRule . /dashboard.php?val=%1&page=idx - [L]
There may be subdomainA and subdomainB, etc so want to do a catch all on subdomains. Hopefully the question makes sense. My Apache knowledge is more limited and most of this is from googling around.
Give the following rules a try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.example\.com$ [NC]
RewriteRule ^/?$ /dashboard.php?val=%1 [NC,L]
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/dashboard\.php [NC]
RewriteRule ^.*$ /dashboard.php?val=%1&page=$0 [NC,L]

redirect any subdomain to main domain with .htaccess

I'm attempting to redirect any direct attempts to access sub.example.com/login over to the original domain/uri at example.com/login, and from the number of questions I've already read in regards to this exact thing, it would appear easy on the surface of it...
I don't know if there's something going on that's overriding the desired outcome, but I have the following rules in my .htaccess file, yet I'm still able to access sub.example.com/login without being redirected.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com/login$ [NC]
RewriteRule ^(.*)$ http://example.com/login$1 [L,R=301]
Actually, just for complete clarity, here's my entire .htaccess file contents. Maybe somebody else can see something wrong that I'm missing.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
# redirect subdomain login attempts to main domain
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com/login$ [NC]
RewriteRule ^(.*)$ http://example.com/login$1 [L,R=301]
Put Redirect subdomain login rewrite condition & rule just after RewriteBase line. That should help to address your issue.
RewriteEngine On
RewriteBase /
# Redirect all subdomains to example.com
RewriteCond %{HTTP_HOST} !^(.*)\.example\.com$ [NC]
# Redirect only bar & foo subdomains to example.com
#RewriteCond %{HTTP_HOST} !^(bar|foo)\.example\.com$ [NC]
RewriteRule ^ http://example.com/ [L,R]

Properly redirecting a subdomain to subdirectory invisibly

I know similar things have been asked before, but none of the solutions I've found have seemed to work out. I'm by far an expert when it comes to mod_rewrite and it's ilk, so apologies if I'm missing something obvious.
I am trying to invisibly redirect subdomains to an index.php file in a subdirectory; this file takes the value of the subdomain as part of the query string, which is working fine.
The problem I'm having is that now everything in this subdirectory is being redirected to the index.php file, which I don't want to happen.
This is what I have thus far:
RewriteEngine On
RewriteBase /
# User dashboards
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^.*$ app/index.php?user=%1 [L,NC,QSA]
What I'm looking for is a situation where http://subdomain.example.com/ will lead to /app/index.php?user=subdomain, but http://subdomain.example.com/assets/stylesheet.css will go to /app/assets/stylesheet.css.
Thanks in advance!
If i understood well your example, you can do it this way:
redirects example.com to www.example.com to avoid having "empty" subdomain
internally rewrites every root subdomains (except www) to /app/index.php?user=subdomain
internally rewrites other things with "app" prefix
Which is represented by this code
RewriteEngine on
# redirects example.com to www.example.com to avoid having "empty" subdomain
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# internally rewrites every root subdomains (except www) to /app/index.php?user=subdomain
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\. [NC]
RewriteRule ^/?$ /app/index.php?user=%1 [L,NC,QSA]
# internally rewrites other things with "app" prefix
RewriteCond %{THE_REQUEST} !app/
RewriteRule ^/?(.+)$ /app/$1 [L,NC,QSA]
EDIT: as you asked in below comments, here is how to manage also www subdomain
RewriteEngine on
# redirects example.com to www.example.com to avoid having "empty" subdomain
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# internally redirects www subdomain root to /site/index.php
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^/?$ /site/index.php [L]
# internally rewrites every other root subdomains to /app/index.php?user=subdomain
RewriteCond %{HTTP_HOST} ^([^.]+)\. [NC]
RewriteRule ^/?$ /app/index.php?user=%1 [L,NC,QSA]
# internally rewrites other things with "app" prefix
RewriteCond %{THE_REQUEST} !app/
RewriteRule ^/?(.+)$ /app/$1 [L,NC,QSA]
Let add second rule to redirect assets to app/assets:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|gif)$ [NC]
RewriteRule ^.*$ app/index.php?user=%1 [L,QSA]
RewriteRule ^assets/(.*)$ app/assets/$1 [L,QSA]
or load all css/js/images directly from app:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^.*\.(css|js|png|jpg|gif)$ app/$0 [NC, QSA]
RewriteRule ^.*$ app/index.php?user=%1 [L,QSA]
EDIT: Sorry, I haven't tested it before, so there is working example:
RewriteRule ^assets/(.*)$ app/assets/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule !^app/assets/ app/index.php?user=%1 [L,QSA]

Redirecting to https with wildcard urls (htaccess)

I have the following problem. I have a site where some pages must be redirected to the https-secured equivalent of the link.
I tried several things but in the end it seems that the server ends up in a loop redirecting :(
Now I redirect every page to the https page but that is not what I want to.
This is the code I use right now:
RewriteCond %{http_host} ^www.domain.nl
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.nl/$1 [L,R=301]
I would like to have al urls starting with bo- have https.
examples:
http://www.domain.nl/bo-users -> redirects to https://www.domain.nl/bo-users
http://www.domain.nl/bo-groups -> redirects to https://www.domain.nl/bo-groups
but
http://www.domain.nl/about-us -> stays at http://www.domain.nl/about-us
It is clear to me I need some wildcards in the rewrite condition for all urls startin with bo-.
We are using apache on the server.
Hope someone can send me in the right direction.
Thanks,
Frank
Update after the tips from Anubhava.
I current;y have this htaccess but I can't get it working (even when clearing the cache in my browser)
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain\.nl$ [NC]
RewriteCond %{HTTPS} on
RewriteRule !^bo- http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
RewriteCond %{HTTP_HOST} ^www.domain\.nl$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^bo- https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
#rewrites een url to www.
RewriteCond %{HTTP_HOST} ^[a-z0-9-]+\.[a-z]{2,6}$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$0 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Hope for some extra help!
Thanks,
Frank
I just opened another call. The initial answer worked fine!
Use this rule:
# force HTTPS for bo- URIs
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.nl$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^bo- https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
# force HTTP for non bo- URIs
RewriteCond %{HTTP_HOST} ^www.domain\.nl$ [NC]
RewriteCond %{HTTPS} on
RewriteRule !^bo- http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]

URL Canonicalization - Apache mod_rewrite

I am probably attempting to take URL Canonicalization a bit too far but here it goes anyway.
Basically I am looking to the following:
301 redirect every base url to http://www.mydomain.com/ some pages are https it should recognize that and continue to use https where already used/requested
301 redirect away any trailing slashes ie http://www.mydomain.com/page/ becomes http://www.mydomain.com/page (I already have a line of code that finds the index.php page - this site is built on Codeigniter)
I Don't want the base url to have the slash stripped that is the only time a slash should be left behind
Find any instances of index.php (in the front middle or end of the url) and 301 redirect them out
ie http://www.mydomain.com/index.php/page/index.php/ becomes http://www.mydomain.com/page
301 redirect any use of my ip address to the actual domain
ie 11.11.111.111/page/index.php would become http://www.mydomain.com/page
Here is what I have so far in my htaccess file in my root directory:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_URI} ^(.*)(/index\.php)$
RewriteRule ^(.*)index\.php/$ http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(mydomain\.com)(:80)? [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^11\.11\.111\.111$
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
#This makes sure that the server actually finds the index file although its not in the url
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
RewriteRule ^(.*)$ /index\.php/$1 [L]
I am stuck right now any help would be greatly appreciated!!
Revisions!!
I have made some progress and here is what I have so far
<IfModule mod_rewrite.c>
RewriteEngine on
# index.php to /
RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP
RewriteRule (.*)index\.(php|html)$ /$1 [r=301,L]
# index.php to / at the base url
RewriteCond %{THE_REQUEST} ^GET\ /index\.(php|html)\ HTTP
RewriteRule (.*)index\.(php|html)$ /$1 [r=301,L]
# force www.
rewritecond %{HTTP_HOST} ^paolienvelope.com [nc]
rewriterule ^(.*)$ http://www.paolienvelope.com/$1 [r=301,L]
# force no IP
RewriteCond %{HTTP_HOST} ^70.40.204.154
RewriteRule ^(.*) http://www.paolienvelope.com/$1 [r=301,L]
#codeigniter direct
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
This successfully forces ip to url
Removes index.php or index.html from the url but correctly directs to the index file despite
makes sure the base url has www.
Still dont have the code to remove the trailing slash from only the request
any help would be appreciated!! Thanks!
RewriteEngine on
# index.php remove any index.php parts
RewriteCond %{THE_REQUEST} /index\.(php|html)
RewriteRule (.*)index\.(php|html)(.*)$ $1$3 [R=301,L]
# force www. (also does the IP thing)
RewriteCond %{HTTP_HOST} !^www\.paolienvelope\.com [NC]
RewriteRule ^(.*)$ http://www.paolienvelope.com/$1 [R=301,L]
# remove tailing slash
DirectorySlash off
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js)
RewriteRule ^(.*)/$ $1 [R=301,L]
# codeigniter direct
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Let's try to solve this one point at the time. As said I'm no pro at this yet but any bit helps i guess:
I'll start with 2. because that one seems easier:
#get rid of trailing slashes
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.\.com$ [NC]
RewriteRule ^(.+)/$ http://www.mydomain.com/$1 [R=301,L]
Does this work, instead of what you have?
source:
http://blog.valtersboze.com/2009/06/add-or-remove-trailing-slash-in-url-with-htaccess/