Very simple Apache htaccess redirect - .htaccess

What I want to do:
Redirect this:
http://example.com/9hyf4dx
... and this:
http://www.example.com/9hyf4dx
to:
http://example.com/special.php?var=9hyf4dx

Try this (it uses a regular expression if the variable you posted is just an example):
RewriteEngine on
RewriteRule ^([a-z0-9]+)*$ special.php?var=$1
if you want you can also add a rule that all visits to your site will have to have the WWW:
#force WWW on main domain
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

Related

Wildcard subdomain doesn't include subfolders

In cPanel (at subdomain management) I've set up a wildcard like to following:
*.example.com > public_html/subDomainHandler
Every (non existent subdomain) request like abc.example.com redirects properly to example.com/subDomainHandler.
But requests like abc.example.com/subfolder doesn't redirect to example.com/subDomainHandler.
I am not at my best in regular expressions, but can anybody give me a RewriteCondition for .htaccess?
Htaccess Rewrite Rule - Try1
I think your answer could be modeled on https://stackoverflow.com/a/8481076/1688441 .
Try the following:
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com\(.*)
RewriteRule ^(.*)$ http://example.com/subDomainHandler/$1 [R=301]
Try2
Another option:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule (.*) http://www.example.com/subDomainHandler%1 [R=301,L]
From: https://webmasters.stackexchange.com/questions/13475/redirect-rewrite-subdomain-to-subfolder

Redirect after a RedirectRule has taken place to other website

I have a website, let's say www.example.com but this used to be www.example.nl. All traffic from www.example.nl is now redirected to www.example.com, but as we changed some naming conventions, www.example.nl/seeds now has to redirect to www.example.com/nl/flower-seeds.
The .htaccess file I got contains the following code:
RewriteEngine On
RewriteBase
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
When I navigate to www.example.nl/seeds/ I end up at www.example.com/seeds/ which ends up in a 404 because I'm missing the /nl/flower- part.
I think Redirect doesn't work properly when the URL is already altered by a RewriteRule. How would you tackle this problem? Any help is appreciated!
You should exclude /seeds/ directory form general rules like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteCond %{REQUEST_URI} !/seeds/(.*)
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^seeds/(.*)$ http://www.example.com/nl/flower-seeds/$1 [L, R=301]
Note: clear browser cache then test
Also , don't use regex with redirect like what you did :
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
Here this (.*) has no meaning , you could use either RedirectMatch or RewriteRule

Add parameter for specific domains in .htaccess rule

I have two domains that point to the same webserver:
example.cz
example.de
I need:
example.de → example.de/de (but can't be see in address field: example.cz/de)
If it ends up showing in the URL like example.de/de it's OK, but the best solution is just example.de and from server load example.de/de.
I've tried with this:
RewriteBase /
RewriteCond %{HTTP_HOST} example.de$ [NC]
RewriteRule ^$ example.de/de [L,R=301]
but after I click on something on the page I get: example.cz/de and this is problem.
You can use an inner rewrite without R=301 redirect, if you use LAMP server, in /.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} (www\.)?example\.de$ [NC]
RewriteRule ^$ /de/index.php [L]
You'll see in address bar: http://example.de but not http://example.de/de .
Add this to redirect http://example.cz/de to http://example.de .
RewriteCond %{HTTP_HOST} (www\.)?example\.cz$ [NC]
RewriteRule ^de$ http://example.de [R=301,L]

.htaccess simple redirection and too many rediercections: how to solve that?

my site (domain.com) is now located into /var/www/
and you can also access it directly through its IP: 88.88.88.88
I would like to obtain:
when user enter the IP : 88.88.88.88, it redirects to 88.88.88.88/reboot/
if user enters the full domain name, it shows the site as it does now.
I tried with .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} 88.88.88.88
RewriteRule .* http://88.88.88.88/reboot/ [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule .* http://www.mysite.com [R=301,L]
does not work good:
www.mysite.com :GOOD
mysite.com: GOOD
88.88.88.88 ->redirects to 88.88.88.88/reboot : GOOD
but then it generates an error: Error 310 (net::ERR_TOO_MANY_REDIRECTS)
Any clue ?
When you're on 88.88.88.88/reboot, your HTTP_HOST is still equal to 88.88.88.88.
Try adding a RewriteCond rule depending on the REQUEST_URI, which will redirect everything that is not /reboot to /reboot :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} 88.88.88.88
RewriteCond %{REQUEST_URI} !^/reboot/
RewriteRule ^(.*)$ http://88.88.88.88/reboot/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule .* http://www.mysite.com [R=301,L]
The ERR_TOO_MANY_REDIRECTS comes because your rule loops.
Whenever a request comes to host 88.88.88.88 , you redirect it to 88.88.88.88/reboot/ , but /reboot/ uri is also on that host, so it redirects again and loops.
If you only want to redirect / to /reboot/ on host 88.88.88.88 , you need to alter your RewriteRule to this:
RewriteCond %{HTTP_HOST} 88.88.88.88
RewriteRule ^/$ http://88.88.88.88/reboot/ [R=301,L]
If, however, you wish to redirect everything outside of /reboot/ , then you need to add a condition (RewriteCond):
RewriteCond %{HTTP_HOST} 88.88.88.88
RewriteCond %{REQUEST_URI} !^/reboot/
RewriteRule ^/$ http://88.88.88.88/reboot/ [R=301,L]
(keep the rest of the rules intact).

.htaccess redirect from subdomain's articles to main domain

I have subdomains: sub1.tld.com, sub2.tld.com and so on. This subdomains contain important info. But I want to prevent opening any other urls, for example: sub1.tld.com/anypage or sub2.tld.com/dir/more/anyurl.html. This urls must be redirected to main domain tld.com: tld.com/anypage or tld.com/dir/more/anyurl.html
So, i have to check 2 things before redirection:
it's any subdomain
it's not subdomain's root page
I created this rules:
RewriteCond %{HTTP_HOST} ^.+\.tld\.com [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*) http://tld.com/$1 [R=301,L]
This set of rules works as expected (redirects sub1.tld.com/anypage to tld.com/anypage) but also redirects sub1.tld.com to tld.com. Where is the error? 2nd string is wrong?
Try this:
RewriteCond %{HTTP_HOST} ^.+\.tld\.com$ [NC]
RewriteRule ^/(.+)$ http://tld.com/$1 [R=301,L]
Or you can use this string to check if it root:
RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /\ HTTP/
For some unknown reason the most reliable for checking variable was %{THE_REQUEST}, not %{REQUEST_URI}.
%{THE_REQUEST} contains raw browser's query to web-server: for example, GET /anypage.html HTTP/1.1, if you are opening url http://anysite.com/anypage.html.
So, here you are complete working rule-set for .htaccess:
RewriteCond %{HTTP_HOST} ^.+\.tld\.com$
RewriteCond %{THE_REQUEST} ^get\ /(.+)\ http.* [NC]
RewriteRule ^(.+) http://tld.com/$1 [R=301,L]

Resources