How to Force domain to with www - htaccess - php

I'm using this htaccess to force all request with www. but my resources don't loaded:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^shadyab.com [NC]
RewriteRule ^(.*)$ http://www.shadyab.com/$1 [L,R=301,NC]
RewriteRule ^ /index.html [L]
this is my site:
http://www.shadyab.com/
for example:
http://www.shadyab.com/assets/plugin/slider/css/owl.carousel.min.css

Your second rule rewrites everything to index.html, including all your css
If you really want to rewrite every request to index.html, but still want your resources, you can exclude them using a condition, otherwise remove the rule.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^shadyab.com [NC]
RewriteRule ^(.*)$ http://www.shadyab.com/$1 [L,R=301,NC]
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpe?g|gif)$ [NC]
RewriteRule ^ /index.html [L]

Related

Htaccess multiple redirect rules should be working

This should be working according to htaccess tester http://htaccess.madewithlove.be/
I'm trying to get url's in the format subdomain.domain.com to resolve to domain.com/index.php?sub=subdomain
However I've also want any links in the form subdomain.domain.com/pagename to redirect to domain.com/index.php?tpl=page&sub=subdomain&url=pagename
At the moment the first rule works if i remove the second rule but if I include both only the second rule works.
Here's the full htaccess
RewriteEngine On
#EDIT: this was messing it all up by appending index.html so the subdomain only
# wasn't triggering at all due to appended pagename
DirectoryIndex disabled
#Remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
#Rewrite if subdomain only
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://example.com/index.php?sub=%1 [P,NC,QSA,L]
#Rewrite if internal page
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com$ [NC]
RewriteRule ^(.+/)?([^/]*)$ http://example.com/index.php?tpl=page&sub=%1&url=$2 [P,NC,QSA,L]
If you answer this I will make you Secretary of State once Flat Earth goes mainstream. Thanks!
Have it like this:
DirectoryIndex disabled
RewriteEngine On
#Remove www
RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/?$ index.php [L]
#Rewrite if subdomain only
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(example\.com)$ [NC]
RewriteRule ^index\.php$ http://%2/index.php?sub=%1 [P,QSA,NC,L]
#Rewrite if internal page
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(example\.com)$ [NC]
RewriteRule ^(?:.+/)?([^/]+)/?$ http://%2/index.php?tpl=page&sub=%1&url=$1 [P,QSA,L]

.htaccess code making a wrong url redirect

This is my .htaccess code
#RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{HTTP_HOST} !hashstar\.com$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
#over here we have set the default root directory now request will be directly made from this directory
RewriteCond %{HTTP_HOST} ^hashstar.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.hashstar.com$
RewriteCond %{REQUEST_URI} !app/
RewriteRule (.*) /app/$1 [L]
#redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
#we are setting here the default file to load while the URL is called followed by fallback files
DirectoryIndex index.php timer/index.php
My root directory is set to folder app
In app folder, I have 2 folders
1.) Manage
2.) Timer
To access manage folder I have to type in URL
http://www.example.com/manage/
To access timer folder I have to type in URL
http://www.example.com/timer/
if I use these Url by adding a slash in URL (/)
the URL loads perfectly
but if the don't add the (/) in URL then it makes a redirection through the root directory
URL should look like => http://www.example.com/manage
It becomes like => http://www.example.com/app/manage/
What's the reason behind it & how to fix this problem?
Any helps appreciated. Thanks in advance.
Reason of this redirect is that rewritten URI is a real directory without trailing slash. When this happens, Apache's mod_dir module acts on it by redirecting to a URI with a trailing slash.
To prevent use this .htaccess:
#we are setting here the default file to load while the URL is called followed by fallback files
DirectoryIndex index.php
#RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{HTTP_HOST} !hashstar\.com$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
#redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
# adds a trailing directory if rewritten URI is a direcory
RewriteCond %{DOCUMENT_ROOT}/app/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]
#over here we have set the default root directory now request will be directly made from this directory
RewriteCond %{HTTP_HOST} ^(?:www\.)?hashstar.com$ [NC]
RewriteCond %{REQUEST_URI} !/app/ [NC]
RewriteRule (.*) /app/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
At a glance (I didn't try to reproduce these rules in my Apache2 server), it would seem that this is the culprit:
RewriteRule (.*) /app/$1 [L]
the Regular Expression (.*) will catch Any character greedily. Your Rewrite Rule is simply adding /app/ between the domain and the script.
Find and group <all non-null characters>,
and transform it to /app/<all non-null characters>
The URL www.example.com/myscript.php would be rewritten as www.example.com/app/myscript.php
EDIT
so what code edit do u suggest? #SsJVasto
You could simply exclude / from the greedy rule:
RewriteRule (.*)/ $1 [L]
This way, you are storing the result of anything ending with a /, but without said / into $1. Since the / isn't inside the group, it will be forgotten.
If you want to handle repetition (like http://www.example.com/myscript.php//), you can add a + sign to handle one or more occurrences:
RewriteRule (.*)/+ $1 [L]

htaccess by adding index.php in the root of the site

This is my current htaccess:
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !^/1EE772B8EBDDECBA378B575830E7E1B3\.txt$
RewriteCond $1 !^(index\.php|assets|wmt|google1f91ad48697ced36\.html|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTP_HOST} ^domain.com.br$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
Options -Indexes
In the first block lines it redirects the http to https;
In the second block it allows some direct access and removes the index.php
In the third block it adds the www if it does not exist.
The ultimate goal is to have urls like:
https://www.domain.com.br ou
https://www.domain.com.br/foo/bar
The problem is that when the user enters the address
Domain.com.br (without www and without the http, only in the root of the site) it redirects to:
https://www.domain.com.br/index.php
Only at the root of the site does it add this index.php
Does anyone know where I'm going wrong?
I accept htaccess optimization suggestions as well
You can use these refactored rules:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_URI} !^/1EE772B8EBDDECBA378B575830E7E1B3\.txt$
RewriteCond $0 !^(index\.php|assets|wmt|google1f91ad48697ced36\.html|robots\.txt) [NC]
RewriteRule .* index.php/$0 [L]
Test it after completely clearing your browser cache.

Rewrite non-www URL to www .htaccess [duplicate]

I would like to redirect www.example.com to example.com. The following htaccess code makes this happen:
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
But, is there a way to do this in a generic fashion without hardcoding the domain name?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Same as Michael's except this one works :P
But if we need to do this for separate http and https:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Redirect non-www to www (both: http + https)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
If you want to do this in the httpd.conf file, you can do it without mod_rewrite (and apparently it's better for performance).
<VirtualHost *>
ServerName www.example.com
Redirect 301 / http://example.com/
</VirtualHost>
I got that answer here: https://serverfault.com/questions/120488/redirect-url-within-apache-virtualhost/120507#120507
Here are the rules to redirect a www URL to no-www:
#########################
# redirect www to no-www
#########################
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
Here are the rules to redirect a no-www URL to www:
#########################
# redirect no-www to www
#########################
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
Note that I used NE flag to prevent apache from escaping the query string. Without this flag, apache will change the requested URL http://www.example.com/?foo%20bar to http://www.example.com/?foo%2250bar
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/(.*)$ https://%1/$1 [R]
The RewriteCond captures everything in the HTTP_HOST variable after the www. and saves it in %1.
The RewriteRule captures the URL without the leading / and saves it in $1.
Try this:
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ %{HTTP_HOST}$1 [C]
RewriteRule ^www\.(.*)$ http://$1 [L,R=301]
If the host starts with www, we stick the whole host onto the start of the URL, then take off the "www."
Complete Generic WWW handler, http/https
I didn't see a complete answer. I use this to handle WWW inclusion.
Generic. Doesn't require domain info.
Forces WWW on primary domain: www.domain.com
Removes WWW on subdomains: sub.domain.com
Preserves HTTP/HTTPS status.
Allows individual cookies for domain / sub-domains
Please let me know how this works or if I left a loophole.
RewriteEngine On
RewriteBase /
# Force WWW. when no subdomain in host
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ [NC]
RewriteCond %{HTTPS}s ^on(s)|off [NC]
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Remove WWW. when subdomain(s) in host
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|off [NC]
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)(.+\.)(.+\.)(.+)$ [NC]
RewriteRule ^ %1%3%4%5%{REQUEST_URI} [R=301,L]
There can be a lot of misinformation out there about htaccess redirects, I find. First off, make sure your site is running on Unix using Apache and not on a Windows host if you expect this code to work.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
(Make sure there are no line spaces between each line of text, though; I have added an extra space between lines so it renders okay in this window.)
This is one snippet of code that can be used to direct the www version of your site to the http:// version. There are other similar codes that can be used, too.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/subfolder/$1 [R=301,L]
For subfolder
www to non www with https
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
For those that need to able to access the entire site WITHOUT the 'www' prefix.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]
Mare sure you add this to the following file
/site/location/.htaccess
Using .htaccess to Redirect to www or non-www:
Simply put the following lines of code into your main, root .htaccess file.
In both cases, just change out domain.com to your own hostname.
Redirect to www
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.tld [NC]
RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]
</IfModule>
Redirect to non-www
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.tld [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]
</IfModule>
I used the above rule to fwd www to no www and it works fine for the homepage, however on the internal pages they are forwarding to /index.php
I found this other rule in my .htaccess file which is causing this but not sure what to do about it. Any suggestions would be great:
############################################
## always send 404 on missing files in these folders
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
############################################
## never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
############################################
## rewrite everything else to index.php
RewriteRule .* index.php [L]
RewriteEngine on
# if host value starts with "www."
RewriteCond %{HTTP_HOST} ^www\.
# redirect the request to "non-www"
RewriteRule ^ http://example.com%{REQUEST_URI} [NE,L,R]
If you want to remove www on both http and https , use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|offs
RewriteRule ^ http%1://example.com%{REQUEST_URI} [NE,L,R]
This redirects
Non ssl
http://www.example.com
to
http://example.com
And
SSL
https://www.example.com
to
https://example.com
on apache 2.4.* you can accomplish this using a Redirect with if directive,
<if "%{HTTP_HOST} =='www.example.com'">
Redirect / http://example.com/
</if>
use: Javascript / jQuery
// similar behavior as an HTTP redirect
window.location.replace("http://www.stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
Or .htaccess:
RewriteEngine On
RewriteBase /
Rewritecond %{HTTP_HOST} ^www\.yoursite\.com$ [NC]
RewriteRule ^(.*)$ https://yoursite.com/$1 [R=301,L]
and The PHP method:
$protocol = (#$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {
header('Location: '.$protocol.'www.'.$_SERVER ['HTTP_HOST'].'/'.$_SERVER['REQUEST_URI']);
exit;
}
Ajax
$.ajax({
type: "POST",
url: reqUrl,
data: reqBody,
dataType: "json",
success: function(data, textStatus) {
if (data.redirect) {
// data.redirect contains the string URL to redirect to
window.location.href = data.redirect;
}
else {
// data.form contains the HTML for the replacement form
$("#myform").replaceWith(data.form);
}
}
});
The only way I got it to work...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^site\.ro
RewriteRule (.*) http://www.site.ro/$1 [R=301,L]
If you are forcing www. in url or forcing ssl prototcol, then try to use possible variations in htaccess file, such as:
RewriteEngine On
RewriteBase /
### Force WWW ###
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
## Force SSL ###
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
## Block IP's ###
Order Deny,Allow
Deny from 256.251.0.139
Deny from 199.127.0.259
This is updated to work on Apache 2.4:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
The only change vs Michael's is to remove the [NC], which produces the "AH00665" error:
NoCase option for non-regex pattern '-f' is not supported and will be ignored
The selected answer and many other solutions here dropped the the part of the url after /, so basically it always redirected to main domain, at least for me.. So i am adding working sample respecting full path after slash..
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [L,R=301]
Alternative approach if .htaccess customization is not ideal option:
I've created simple redirect server for public use. Just add A or CNAME record:
CNAME r.simpleredirect.net
A 89.221.218.22
More info: https://simpleredirect.net
I am not sure why u want to remove www.
But reverse version would be:
# non-www.* -> www.*, if subdomain exist, wont work
RewriteCond %{HTTP_HOST} ^whattimein\.com
RewriteRule ^(.*)$ http://www.whattimein.com/$1 [R=permanent,L]
And advantage of this script is:
if u have something like test.whattimein.com or any other (enviroments for developing/testing)
it wont redirect U to the original enviroment.
Added if localhost, ignore redirection (for development purpose in local environment). If not localhost AND (not https OR it’s www), redirect to https and non-www.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !localhost [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Add
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
to your .htaccess before any other rule.
Hi you can use following rules on your htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

htaccess in subdirectory for parked domain

Motive: What I am trying to do is use parked domain and redirect it to subdirectory. and in subdirectory I have a script that needs pretty url's and htaccess is the only thing I can use.
Code in main .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^info/(.+)$ contact.php?a=$1
RewriteRule ^list/(.+)/page/(.+)$ list.php?a=$1&p=$2 [L]
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^(.*)$ http://www.example.com/m/$1 [R=301]
Code in /m .htaceess
RewriteEngine On
RewriteBase /
RewriteRule ^how/(.+)$ how.php?a=$1
RewriteCond %{HTTP_HOST} ^(www.)?main-example.com$ [NC]
RewriteCond %{REQUEST_URI} ^/m/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
What i want is to achieve when user visits www.example.com/m/how/love the script should treet a get variable of a something like www.example.com/m/how.php?a=love but url should
remain pretty.
What my current code does is unlimited redirects to /m/404.shtml giving 301 in firebug.

Categories