Two rules at the same time and their order - .htaccess

I have aim to use two domains (old and new). So when I go to address:
http://old.cz/whatever/whatever
I would like to get to:
http://new.cz/whatever/whatever
Perfectly works for me this thing:
RewriteRule ^(.*)$ http://www.new.cz/$1 [R=301]
But! At the same time I want following. When I go to address:
http://old.cz/
I want to get to:
http://new.cz/specific-page/specific-page
For that works this code:
Redirect 301 / http://new.cz/specific-page/specific-page
My issue is that in case I use both rules at the same time the first one is always prioritize and the second one suppressed. It means that when I go to http://old.cz/ I always get on only to http://new.cz/
Help me please.

Redirect and RewriteRule are directives of two different apache modules mod-alias and mod-rewrite . You can not combine these two directive for url redirection because of their different runtime behaviour. Use RewriteRule instead of Redirect.
RewriteEngine on
RewriteRule ^/?$ http://new.cz/specific-page/specific-page [L,R]
RewriteRule ^(.*)$ http://www.new.cz/$1 [R=301]

Related

Rewriting Dynamic URLs in htaccess not working

I want to rewrite
mypage.com/country/country.php?country=something
to
mypage.com/country/something
in the address bar, using htaccess
I've tried many things and looked everywhere and the closest I've got is:
RewriteCond %{QUERY_STRING} country=([^\&]*)
RewriteRule ^country.php$ /country/%1? [R,L]
But this just produces a rewrite loop that alternates between the two links above and I don't understand why.
I want both
mypage.com/country.php?country=something
and
mypage.com/country/something
when entered, to show
mypage.com/country/something
in the address bar
Any help?
If you have a rewrite loop it suggests that besides that rule, you also have a rule that translates it back. You'll need the 'ugly' url to only trigger when it is an external request. The easiest way to do that is by matching %{THE_REQUEST}.
#Ugly to fancy url; should be R=301 when it works
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /country\.php\?country=([^\&]*)\ HTTP
RewriteRule ^country.php$ /country/%2? [R,L]
RewriteRule ^country/(.*)/?$ /country.php?country=$1 [L]

.htaccess Redirect + Rewrite in one rule

I have a rewrite rule to hide index.php, which is
working fine.
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I am currently redirecting a specific sub-domain to another domain.
RewriteCond %{HTTP_HOST} ^(www\.)?deutschland\.example\.com$ [NC]
RewriteRule ^ http://www.example.de%{REQUEST_URI} [NE,R=301,L]
The redirect is working fine, but now I am getting index.php in the URL too, which is coming in the REQUEST_URI.
http://www.example.de/index.php/search/result
So how to remove 'index.php' from this redirected URL?
Note: Its the same php website application only, just using country-wise multiple domains.
(1) Rule order is important. (2) the last flag doesn't mean last; it means last on this cycle. (From Apache 2.4 the end flag does what you might think last does. See my Tips for debugging .htaccess rewrite rules for more discussion of this). So in this case rule(1) fires and then mod_rewrite loops around again and this time rule (2) fires giving what you find.
Swap the two rules around and it will work as expected.

htaccess subdomain rewrite without a redirect

Using htaccess Rewrite, I want my url http://*.phoneataxi.com/ (where * is a wildcard, excluding 'www') to show in the address bar as is but get information from http://*.phoneataxi.com/test.php?c=*.
I have tried so many different things but nothing is doing exactly what I need. Most examples are redirecting the subdomain to the '/test.php' file in the address bar which I don't want to do.
I'm trying not to have to create individial subdomains and subdomain folders within my webroot.
Ideas?
I use this htaccess file to make Apache act as a proxy for another host:
IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ghost\.pileborg\.se$
RewriteRule (.*) http://vps.pileborg.se/ghost/$1 [P]
</IfModule>
It causes all access to http://ghost.pileborg.se/ to be "redirected" to http://vps.pileborg.se/ghost/.
UPDATE (2020)
Some of the answers regarding this topic is very old and no longer work as expected.
After searching for hours on something that actually works, this is what I came up with; edit as you see fit:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ([a-z0-9]+)\.
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.php -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.html -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.html [L,NC,QSA]
Breakdown
Make sure that the rewrite module is installed and enabled on your host
first we turn the rewrite engine on and set the path-base
then isolate the subdomain - any letters/numbers before the first dot
set a variable in this runtime environment that contains the subdomain
check if the subdomain folder and index-file exists
if it does exist -then use that file as the request-handler (no redirect)
if it does not exist then the request carries on normally
Flags
The flags used here are explained here, but the ones used above are quite simple:
[L] Last rule, ignore the rest
[NC] No Case, no uppercase/lowercase restrictions
[QSA] I remember this as "Query String Attach" :D

.htaccess redirect from one subfolder to other subfolder

I know this sounds like so many other questions here, but I can't seem to find the answer.
Say you are on:
www.domain.com/folderA/folder2/folder3/
I want that to redirect to:
www.domain.com/folderB/folder2/folder3/
So the whole structure stays the same.. it just redirects.
Now so far I have:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/folderA [NC]
RewriteRule ^(.*)$ /folderB/$1 [R=301,L]
But when I use that, it'll just do
www.domain.com/folderB/folderA/folder2/folder3/
What am I doing wrong? How do I get rid of that folderA?
The pattern ^(.*)$ includes also the prefix folderA. You must specify folderA explicitly in the pattern and capture only the latter part in the RewriteRule. Then you can drop the RewriteCond
RewriteEngine on
RewriteRule ^/?folderA/(.*)$ /folderB/$1 [R,L]
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

htacces redirect and mask

How would I redirect from the root folder to a sub folder and then mask that folder?
So instead of http://root.com/sub_folder
It would be just http://root.com
I have tried:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^root\.com$
RewriteRule (.*) http://root.com/$1 [R=301,L]
RewriteRule ^$ /sub [L]
However, that does not work. Any help will be welcome.
To clarify what I think you're looking for:
You want users who enter http://root.com with no trailing path to be rewritten silently to http://root.com/sub.
If a user directly enters http://root.com/sub, however, you want them to be redirected to http://root.com.
Any other path within root.com should be left alone.
The following two rules accomplish this. If you have more than one domain and only want this to apply to one domain, add your original RewriteCond in front of each RewriteRule.
RewriteRule ^sub/?$ http://root.com/ [R=301,L]
RewriteRule ^$ /sub [END]
First rule redirects /sub with or without trailing slash to root.com. Second rule rewrites base domain to /sub.
EDIT: Per Jon Lin's comment, below, the [L] flag only stops the current round of processing and internal rewrites are sent through the rules once more (I always forge that part). So, you can terminate the second line with [END] instead, which stops all rewrite processing. The catch is that [END] is only available in Apache 2.4 or higher, so if you're on an older version something trickier will need to be done.

Resources