Rewrite URL using Apache for redirecting root domain - apache

I have this scenario where I would like to redirect my domains using the following scenario. Can you please advice if this can be achieved using RewriteRule in Apache?
I would like to redirect any calls made using http://www.domainname.com/url to redirect to http://domainname.com/url.
I was able to achieve the above using the following rewrite rule
# BEGIN WithoutWWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]
# END WithoutWWW
If anyone tries to visit just http://www.domainname.com or http://domainname.com, I would like them to redirect to http://dname.com
How can I achieve this rule using RewriteRule in Apache?
I'm also using PHP, so a solution using PHP would be valid too.

Here is your combined .htaccess with your existing and new code:
RewriteEngine on
# redirect domainname.com/ or www.domainname.com/ to dname.com/
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.com$ [NC]
RewriteRule ^$ http://dname.com [R=301,L]
# append www to domainname.com
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule ^ http://domainname.com%{REQUEST_URI} [R=301,L]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]

That example looks like mod_rewrite.
With mod_rewrite you could do:
# BEGIN WithoutWWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.com$ [NC]
RewriteRule ^(.*)$ http://dname.com/$1 [R=301,L]
# END WithoutWWW
You may want to look at http://www.workingwith.me.uk/articles/scripting/mod_rewrite
Based on your comment you want to redirect the root differently so we could do:
RewriteEngine on
#www.domainname.com & domainname.com -> dname.com
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.com$ [NC]
RewriteRule ^$ http://dname.com/ [R=301,L]
#www.domainname.com/[url] -> domainname.com/[url]
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]

Related

Rewrite Rule in Apache/htaccess not working

I'm trying to redirect user request to my domain in .htaccess file.
My requirement is for example if user request for example.co/abc it should redirect to www.example.co/abc.
Below is my rewrite rule.
RewriteCond %{HTTP_HOST} ^example.co/Abc [NC]
RewriteRule ^(/Abc)$ https://www.example.co/$1 [L,R=301]
After adding the above two lines in my .htaccess file it is redirecting from example.co to www.example.co
I tried in different ways still not getting where i'm doing mistake.
Any help would be appreciated.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co [NC]
RewriteRule ^(.*)$ http://www.example.co/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{www.example.co} [L,R=301]
RewriteCond %{HTTP_HOST} ^example.co [NC]
#RewriteRule ^(/*)$ https://www.example.co/$1 [L,R=301]
RewriteRule ^/?(Abc)/?$ https://www.example.co/$1 [L,R=301]

How to combine different apache redirects?

I am trying to combine different apache redirects. I thought just getting rid of any Last flag would do the trick but it didn't (see How to combine Apache redirects?). It makes the following two landing page redirects
example.org -> www.example.org -> www.example.org/products/
But I'd rather have
example.org -> www.example.org/products/
to avoid delays.
Here is the .htaccess file:
# FORCE WWW
RewriteCond %{HTTP_HOST} !^www\.example\.org$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://www.example.org/$1 [R=301]
# REDIRECT FROM ROOT TO PRODUCTS(.PHP)
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^$ /products/ [R]
RewriteRule ^products/$ /products/products.php
# ADD TRAILING SLASH
RewriteRule ^(([^/]+/)*[^./]+)$ http://www.example.org/$1/ [R=301]
# INTERNAL REWRITE OF "products/myname/myversion/" to "products/products.php?name=myname&version=myversion"
RewriteRule ^products/([a-z0-9-]+)/([0-9-]+)/$ products/products.php?name=$1&version=$2 [NC]
RewriteRule ^products/([a-z0-9-]+)/$ products/products.php?name=$1 [NC]
Try it like this currently I did not tried it,
#FORCE WWW with products
RewriteCond %{HTTP_HOST} ^example.org [OR]
RewriteCond %{REQUEST_URI} !^products
RewriteRule ^(.*)$ http://www.example.org/products/$1 [R=301]
And remove these lines
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^$ /products/ [R]

htaccess rewrite from a php file to subdomain

I would like to redirect from example.com/profile.php?UserName=xxxx to xxxx.example.com
xxxx contains a-z or 0-9.
I have tried bunch of codes to do it (some from this site) but none of them worked as I wanted it to be. Here is my current code:
RewriteEngine on
#this should redirect example.com/profile.php?UserName=x to x.site.com
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule (.*) %1/$1 [QSA,L]
#if link does not contain subdomain add www
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
You need to put all of your redirect rules before your internal routing rules. The rules with the R flag redirect.
RewriteEngine on
# This redirects the browser to include the "www"
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# This redirects the browser when profile.php is directly accessed
RewriteCond %{THE_REQUEST} \ /+profile\.php\?UserName=([a-z0-9-_]+)
RewriteRule ^ http://%1.example.com/? [L,R]
# this internally routes the XXX.example.com to the profile.php script
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule ^$ /profile.php?UserName=%1 [QSA,L]
Since there was not a good answer here.. In order to rename a php file and use the subdomain extention.. I had to edit my DNS settings.. This tutorial helped :http://www.mediacollege.com/internet/server/apache/mod-rewrite/subdomains.html

.htaccess rewrite subdomain to directory

Is it possible to use .htaccess to rewrite a sub domain to a directory?
Example:
http://sub.domain.example/
shows the content of
http://domain.example/subdomains/sub/
Try putting this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.example
RewriteRule ^(.*)$ /subdomains/sub/$1 [L,NC,QSA]
For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.example
RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA]
I'm not a mod_rewrite expert and often struggle with it, but I have done this on one of my sites. It might need other flags, etc., depending on your circumstances. I'm using this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomains/subdomain
RewriteRule ^(.*)$ /subdomains/subdomain/$1 [L]
Any other rewrite rules for the rest of the site must go afterwards to prevent them from interfering with your subdomain rewrites.
You can use the following rule in .htaccess to rewrite a subdomain to a subfolder:
RewriteEngine On
# If the host is "sub.domain.example"
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
# Then rewrite any request to /folder
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
Line-by-line explanation:
RewriteEngine on
The line above tells the server to turn on the engine for rewriting URLs.
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
This line is a condition for the RewriteRule where we match against the HTTP host using a regex pattern. The condition says that if the host is sub.domain.example then execute the rule.
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
The rule matches http://sub.domain.example/foo and internally redirects it to http://sub.domain.example/folder/foo.
Replace sub.domain.example with your subdomain and folder with name of the folder you want to point your subdomain to.
I had the same problem, and found a detailed explanation in http://www.webmasterworld.com/apache/3163397.htm
My solution (the subdomains contents should be in a folder called sd_subdomain:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} subdomain\.domain\.example
RewriteCond $1 !^sd_
RewriteRule (.*) /sd_subdomain/$1 [L]
This redirects to the same folder to a subdomain:
.httaccess
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.example$ [NC]
RewriteRule ^(.*)$ http://domain\.example/subdomains/%1
Try to putting this .htaccess file in the subdomain folder:
RewriteEngine On
RewriteRule ^(.*)?$ ./subdomains/sub/$1
It redirects to http://example.org/subdomains/sub/, when you only want it to show http://sub.example.org/.
Redirect subdomain directory:
RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301]
For any sub domain request, use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.band\.s\.example
RewriteCond %{HTTP_HOST} ^(.*)\.band\.s\.example
RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-z\-]+)
RewriteRule ^(.*)$ /%1/$1 [L]
Just make some folder same as sub domain name you need.
Folder must be exist like this: domain.example/sub for sub.domain.example.
Edit file .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.htaccess redirect without change url same host

I want to redirect a subdomain inside panel, the subdomain editor.domain.com
It should redirect to domain.com/cmseditor.
However the visitor should see editor.domain.com.
So far I've tried:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^editor\.homecatering\.pt$ [NC]
RewriteRule ^ http://homecatering.pt/cmseditor%{REQUEST_URI} [L,R=301]
Try something like this:
RewriteEngine On
# If the subdomain is "editor.homecatering.pt"
RewriteCond %{HTTP_HOST} ^editor.homecatering.pt$ [NC]
# Then rewrite any request to directory /cmseditor
RewriteRule ^((?!cmseditor).*)$ /cmseditor/$1 [NC,L]
or
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.homecatering\.pt$ [NC]
RewriteRule ^(.*)$ http://homecatering\.pt/cmseditor/%1