.htaccess Redirect all Subdomains to an URL - apache

how can I redirect all of the subdomains to a URL
this is the scenario :
abc.xxx.com goes to abc.xxx.com/index.php?id=abc
www.xxx.com/abc or xxx.com/abc also goes to the same URL: abc.xxx.com/index.php?id=abc
and please bear in mind there is no /abc folder on the website
and the file index.php is the root file of the main domain
I want to keep the abc in the subdomain for SEO purpose.
Thanks in advance.

I think you must first check if URL does not contain "index.php?id=" to prevent infinite loop.
RewriteCond %{REQUEST_URI} !index\.php\?id= [NC]

Related

Conditional redirecting rules in .htaccess

I need to apply the following rules in my .htaccess file-
Don't redirect if the url is mysite.com
Don't redirect if the url is mysite.com/index.php or mysite.com/anypage.anyextension
Don't redirect if the url has the word admin after domain as mysite.com/admin/...
Redirect if the url has the pattern mysite.com/anyword/ or mysite.com/anyword to mysite.com/somewhere/somepage.php?slug=anyword
.htaccess is a completely new world to me.
Could someone help?
If you are looking to have a specific folder perform a redirect, for example a news folder the template would be
RewriteEngine On
RewriteRule ^news/([A-Za-z0-9-]+)/?$ /news/index.php?slug=$1 [NC,L,QSA] # Process posted item
This redirects yousite.com/news/my-first-article to yoursite.com/news/index.php?slug=my-first-article
Just be sure to replace news and index.php with the pages actually handling the command

htaccess url redirect with subdomain and path

I've got two subdomains connected to two domains and a path and I need to redirect to a a new domain with the same subdomain without the path i.e.
www.subdomainone.olddomain.com/path/article -> www.subdomainone.newdomain.com/article
www.subdomainone.olddomaintwo.com/path/article -> www.subdomainone.newdomain.com/article
www.subdomaintwo.olddomain.com/path/article -> www.subdomainone.newdomain.com/article
www.subdomaintwo.olddomaintwo.com/path/article -> www.subdomaintwo.newdomain.com/article
I am not familiar with htaccess rules and I have tried using the testers online but cannot solve my problem
You can use this generic rule on olddomain's site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.olddomain\.com$ [NC]
RewriteRule ^(?:fr(?:-ca)?/)?(.*)$ http://www.%1.newdomain.com/$1 [L,NC,R=302]
Apache has url rewrites set of simple regex that let you do any king of such operation, all incomming traffic URL wil be rebuild before pointing to the right path (if is that what are you looking for).
Write storng rules that do not rebuild old URLs that stay the same.
A mor trustes solution (as you migrated to new urls) is to rewrite all old URLs but not prompt users to the page but rather sending to the user a redirect HTTP to the new url so you avoid the physical user using them and they will be forgotten in a short time.
example request:
http://subdomain.olddomain.com/path/article/pme
htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.olddomain\.com$
RewriteRule ^(?:path(?:-pathy)?/)?(.*)$ http://%1.newdomain.com/$1 [R=301,QSA,L]
This accounts for a hyphen in the path also for a dynamic subdomain.
new url: http://subdomain.newdomain.com/article/pme

.htaccess or cPanel redirects. Redirect pagename in folder to subdomain/pagename (all folder contents)

I am trying to get a redirect set up for my site. I have three subdomains which I use
1) (www.)mysite.com
2) sub.mysite.com
3) anothersub.mysite.com
I initially didn't use subdomains and had all my pages in a www.mysite.com/*.php format.
I am trying to add a redirect to make all the pages under the /myfolder/ folder redirect to sub.mysite.Com/ (NB sub is the same name on my site as the myfolder folder)
I have tried setting up a redirect through cPanel but all the combinations have resulted in 404 errors when I try them. I have also tried various things in .htaccess which resulted in many 404 errors.
I added a simple redirect in cPanel and get the following
e.g.
www.mysite.com/myfolder/myfolder_mypagename.php
should redirect to
sub.mysite.Com/myfolder_mypagename.php
but instead it redirects to
sub.mysite.com/_mypagename.php (missing out the "myfolder" part of the pagename)
Can you assist me in this please? It's driving me bonkers and my hosting company said it wasn't possible (which I find hard to believe!)
This answer is assuming your are facing the issue that, when creating a subdomain through cpanel and pointing it to the desired directory, some part of the urls get striped.
Create you subdomain through cpanel, and make it point to the root directory (just like www.mysite.com), then use this in your htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$
RewriteRule ^myfolder/(.*)$ http://sub.mysite.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^sub\.mysite\.com$
RewriteRule ^(.*)$ myfolder/$1 [L,QSA]

301 redirect not working

I made a subdomain, and I am trying to redirect a page from the original domain to the subdomain, with the same dir structure.
My original page is this:
http://www.comehike.com/outdoors/alaska_hiking.php
I put this rule into the .htaaccess file under comehike.com/outdoors/ directory:
RewriteRule ^outdoors/alaska_hiking.php http://hiking.comehike.com/outdoors/alaska_hiking.php [R,L]
But as you can see from visiting the original url, it doesn't redirect. Any idea why?
Thanks!
I use apache server.
That RewriteRule line works for me when I stick it in my .htaccess file, are you sure you have RewriteEngine On?

How can I redirect all traffic from one domain to another with an .htaccess file?

Say I have a subdomain xxx.yyy.com running Apache. The files are stored in /home/someone/public_html/xxx.
What I want to do is redirect all requests to a domain name zzz.com which is using the same location for its files. (In other words, xxx.yyy.com and zzz.com are aliases for each other)
I just want people accessing zzz.com, so if someone goes to xxx.yyy.com they should be redirected to zzz.com.
Can this easily be done with a rewrite rule in an .htaccess file?
Try
RewriteCond %{HTTP_HOST} ^xxx\.yyy\.com
RewriteRule (.*) http://zzz.com/$1 [R=permanent,QSA,L]