I want to add "www." to URLs if it doesn't contain "www." after "http://"; but it shouldn't get added if any file is requested from some specific folders.
Here is what I have tried.
RewriteEngine On
RewriteCond %{PATH_INFO} ^/exception_dir(.)*$
RewriteRule ^(.*)$ - [L]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
With this code, presently, the "exception_dir" is NOT being exempted from redirection. What I am missing?
And how to write RewriteCond and/or RewriteRule if there are multiple directories to be exempted? Do I need to write both RewriteCond and RewriteRule for each of the directories separately?
You can do your rules like this. In this example I also excluded 2 directories. You can do 1 or several directories.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/exception_dir1 [NC]
RewriteCond %{REQUEST_URI} !^/exception_dir2 [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
Related
I am new to this.
Code from my .htaccess file goes like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
Redirect 301 /abc/ /abcnew/
I want this to redirect from www to non-www i.e., from http://www.example.com to http://example.com
I copied:
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
this code from here Generic htaccess redirect www to non-www.
I also checked in /etc/apache2/mods-enabled folder on my linux server. There "rewrite.load" this module is present.(I think this might mean that rewrite is enabled on my server, but correct me if I am wrong.)
Redirect 301 /abc/ /abcnew/
and just FYI this above code works fine(its redirecting my old links to new links).
I also tried this.
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Doesn't work for me.
Please help. Thanks in advance...
Edit:
this link I found this. But not sure what should be edited. Can anyone please point out.?
You need to place external (full) redirect rules before internal rewrite ones and also make sure to use mod_rewrite rules only.
Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
RewriteRule ^abc/?$ /abcnew/ [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
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]
I would like all requests to be directed to the root page. I also need www to be added if it has been missed out. I have tried numerous attempts but can not seem to get it to work, latest attempt
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [L,R=301]
RewriteRule !^.*/ / [NC,L]
This code below works providing that they do not use www within their requests.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [L,R=301]
You can use this single rule as your first rule in your DOCUMENT_ROOT/.htaccess file::
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{REQUEST_URI} ^/.+
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ http://www.domain.com/? [L,R=301]
I have multiple domain that point to the same server. Then i need to separate it to different directory. how to make it in htaccess or other way.
Here an example.
domain1.com show 1.2.3.4/abc
domain2.com show 1.2.3.4/def
domain3.com show 1.2.3.4/ghi
...
You can use these type of rules in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC]
RewriteRule !^abc/ /abc%{REQUEST_URI} [L,NC]
RewriteCond %{HTTP_HOST} ^domain2\.com$ [NC]
RewriteRule !^def/ /def%{REQUEST_URI} [L,NC]
RewriteCond %{HTTP_HOST} ^domain3\.com$ [NC]
RewriteRule !^ghi/ /ghi%{REQUEST_URI} [L,NC]
My .htaccess in a folder looks like:
RewriteEngine On
RewriteBase /profile/
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?username=$1
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/profile/$1 [R=301,L]
Basically, if you go to www.mySite.com/profile/Username, my index.php file takes 'Username' as a $_GET variable, and the URL will look clean (www.mySite.com/profile/Username)
However if you go to mySite.com/profile/username (omitting the www), the URL will look like http://www.mySite.com/profile/index.php?username=username
How can I make it so only the www is added without messing the URL up?
Thanks
Ordering of rules does matter in .htaccess.
Try this code instead:
RewriteEngine On
RewriteBase /profile/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/profile/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?username=$1 [L,QSA]
I'm currently working on a blog portal, and I'm trying to achieve a specific thing here.
This is my .htaccess code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.[NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.MyDomain.com$ [NC]
RewriteRule ^$ blogdisplay.php?page=%1 [L,QSA]
RewriteRule ^([a-zA-Z0-9-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ index.php?page=$1&page2=$2
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ index.php?page=$1&page2=$2
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ index.php? page=$1&page2=$2&page3=$3
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ index.php?page=$1&page2=$2&page3=$3
When a person is trying to reach SOMETHING.mydomain.com, htaccess is rewriting it to blogdisplay.php?page=BLOGNAME. Just as planned. Now, the problem is that I want it to behave exactly like the main website and rewrite SOMETHING.mydomain.com/page to blogdisplay.php?page=BLOGNAME?page2=page.
Is there any way to make the subdomain use blogdisplay.php instead of index.php, but otherwise work exactly like the subpages of the "main website"?
Replace your code with this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.[NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.MyDomain.com$ [NC]
RewriteRule ^$ blogdisplay.php?page=%1 [L,QSA]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f
## don't do anything
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.MyDomain\.com$ [NC]
RewriteRule ^([a-z0-9-]+)/?$ blogdisplay.php?page=%1&page2=$1 [L,QSA]
RewriteRule ^([a-z0-9-]+)/?$ index.php?page=$1 [L,NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/?$ index.php?page=$1&page2=$2 [L,NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ index.php?page=$1&page2=$2&page3=$3 [L,NC]