Redirect subdomain only to root - regex

So I have this website: www.example.com, which in the past would redirect the clients to www.example.com/subdir
Now I updated the whole site, and the subdirectory doesn't exist anymore, and I want the main domain to be the root. The problem is that many clients have cached the url with the subdomain included, so I need a redirection rule to route www.example.com/subdir to www.example.com.
I tried this:
RewriteRule ^subdir/(.*)$ $1
which works if I enter www.example.com/subdir/ with the last slash
but if I access www.example.com/subdir without last slash it won't do the job.
How can I make it work?

Change your rule to this:
RewriteRule ^subdir(?:/(.*))?$ /$1 [L,NC,R=301,NE]
It is important to use R=301 here so that all clients and search engines eventually move to new URL rather than keep using duplicate URL for the same content (bad SEO).

Related

Redirect or rewrite any subdomain to www leaving the rest of URL as it is

I have:
subdomainaaa.mydomain.com
subdomainaaa.mydomain.com/anything/whatever
subdomainbbb.mydomain.com
subdomainbbb.mydomain.com/anything/whatever
...
...
anysubdomain.mydomain.com ...
Practically wild card subdomain.
And I need rewrite rule to make it all:
www.mydoamin.com/and_whatever_goes_afterwords
Basically, how to redirect zillion subdomains to www, leaving anything that goes after .com/ as it is.
I must do some sort of permanent redirection because I can't lose old links, just to redirect them to new ones with www instead of subdomains
Please, I was looking all over the internet for this, and I get only chunks of regex which I don't know how to put together, and whatever I mix in my .htaccess file - I get server error. Or, I manage to redirect anything just to the homepage www.mydomain.com. I hope I was clear. I am a regex newbie (wannabe), so, please give me the whole chunk to put in .htaccess.
Or, if you think I am doing something wrong - give me suggestion how to accomplish my goal.
Thanks a lot guys!!
You may use this rule in your site root .htaccess:
RewriteEngine On
# add www and keep same URI
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.(mydomain\.com)$ [NC]
RewriteRule ^ http://www.%1%{REQUEST_URI} [R=301,L,NE]

htaccess redirect to domain based on string in url

We have a site where content is generated from CMS. The issue is people have added links to their website without the http:// hence all the links got generated in a wrong way.
for eg: http://www.example.com/ec/shefaliaxena/eventupdate/www.shefalisaxena.com
What I would like to do is get the data after last slash and check if it is valid domain using regex and if it is valid domain then redirect it to that domain or else do nothing.
I was able to extract the data after last slash using regex ([^/]+$) but wondering how do I acheieve this redirect.
I could do this easily with the coding but I would like to handle this from .htaccess file itself so that it doesn't add burden on the server.
A problem you might run into trying to do this is that valid filenames often look a lot like valid domain names, so you'll want to make sure something that looks like a domain name isn't a valid file before trying to forward the URL:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /(([A-Za-z0-9-]+\.)+[A-Za-z]{2,6})$ http://$1/ [L,NC,R=301]
If you're generating the URLs from data users have entered, I'd definitely look at adding the protocol there instead of doing it this way.

How 301 Redirect any URL that has a certain variable?

I have an old site to clean up that has many pages with this ugly url format:
/index.php?option=com_content&view=article&id=47&Itemid=63
How do I write a rule in the .htaccess file that redirects any url that starts with ?option= to root /
Any suggestions would be great.
Thanks,
Why bother with rewrite rules, if you can also patch it in the index.php file itself which presumably still exists within the new site?
if(!empty($_GET['option']))
{
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://my.domain.tld/');
die;
}
This also correctly catches URLs that historically existed but had parameters switched or prefixed so that option wasn't at the beginning, and allows you to more flexibly patch if one day you need to use an option GET-parameter again.
Mod_rewrite is cool, but not the solution to every redirection problem.
Having said that, if you truly insist on the .htaccess solution, try this:
RewriteCond %{QUERY_STRING} ^option=(.*)
RewriteRule . / [L,R=301]
It tests the query string and then redirects all requests if it matches.

how to generate an internal redirect of a google gclid url which is causing a redirect loop?

for
joomla 1.5.26 website
running virtuemart 1
and SH404SEF 2.5.0.1040 enabled.
"Google Ads" urls returned to the site from a google campaign are causing a redirect loop:
here is an example:
http://www.brand-it.co.il/%D7%94%D7%93%D7%A4%D7%A1%D7%94-%D7%A2%D7%9C-%D7%9B%D7%9C%D7%99-%D7%9B%D7%AA%D7%99%D7%91%D7%94/%D7%94%D7%93%D7%A4%D7%A1%D7%94-%D7%A2%D7%9C-%D7%A2%D7%98%D7%99%D7%9D.html/?gclid=CKyLgNSjxbkCFbHKtAoaaaaaa
i upgraded the SH404SEF and isolated the problem to a trailing forward slash after the .html part of the url
so instead of this part .html/?gclid=
i remove the slash like this .html?gclid=
and it works fine:
http://www.brand-it.co.il/%D7%94%D7%93%D7%A4%D7%A1%D7%94-%D7%A2%D7%9C-%D7%9B%D7%9C%D7%99-%D7%9B%D7%AA%D7%99%D7%91%D7%94/%D7%94%D7%93%D7%A4%D7%A1%D7%94-%D7%A2%D7%9C-%D7%A2%D7%98%D7%99%D7%9D.html?gclid=CKyLgNSjxbkCFbHKtAoaaaaaa
so my question is: how to keep the original url indexed and create an internal redirect from the non-working url to the working one ? using .htaccess ?
thanks
You can try this rule:
RewriteEngine On
RewriteRule ^(.+?\.html)/$ /$1 [L,NC,NE,R=301]

Redirect deep link URLs to new domain with htaccess

Say for example you currently host a site www.domain.com and you are going to move it to www.new-domain.com. Now, currently you're blog structure is www.domain.com/2012/09/post-name and your new blog structure will be www.new-domain.com/blog/post-name. I know I can do a 301 permanent redirect on domain.com to point to new-domain.com, but is there a way to capture the long-tail URL and RegEx a rewrite with htaccess. Usually I would do:
RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/(.*)$ http://domain.com/blog/$3
If I need to change URL structure within the same domain. Will this catch redirects from the old domain also?
I'm assuming you've got both domains pointing to the same directory otherwise asking about redirects from the old domain wouldn't make any sense.
If you've got a redirect on the new domain document root, you'd never ever see requests from the old domain, and vice versa.
If both domains share the same document root, you'll need to do the check against the host:
RewriteEngine On
RewriteCond %{HTTP_HOST} domain.com$ [NC]
RewriteRule ^/?[0-9]{4}/[0-9]{2}/(.*)$ http://new-domain.com/blog/$1 [L,R=301]