Set htaccess to different directories - .htaccess

I need to configure my htaccess so that it can be answered in two different ways.
Ex: foo.com access the path /foo (foo.com only) and when;
foo.com/any access the path /internal.
How can I do this?

RewriteEngine On
RewriteRule ^foo$ foo.com [QSA,L,NC]
RewriteRule ^([0-9A-z_-]+)$ /internal [QSA,L,NC]
This resource might help you:
Ask Apache
You could use whatever was typed and print in the url as a variable
like:
RewriteRule ^([0-9A-z_-]+)$ /internal?area=$1 [QSA,L,NC]

Related

How to hide folder in url bar

I need to hide folder name in my url bar.
Now I have mateotxt.pl/website/regulamin.html
But I need
mateotxt.pl/regulamin.html
I need to skip this folder name in my url bar.
This probably is what you are looking for:
RewriteEngine on
RewriteRule ^/?regulamin\.html$ /website/regulamin.html [END]
In case you are looking for a general rule that might be of help:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/website/
RewriteRule /website%{REQUEST_URI} [END]
Though you need to take care that you do not actually rewrite resources you do not want to get rewritten that way. So maybe resources stored in other folders in the server side file system...
You should implement those directives in the actual http server's host configuration. If you do not have access to that you can use a distributed configuration file (often named ".htaccess"), but that comes with a performance penalty...
you can try the following:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^website/
RewriteRule ^(.*)$ website/$1 [L]
try this
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/string/
RewriteRule ^string/(.*)$ https://www.domain.eu/$1 [L,NC,R=301]

Need help in URL rewriting sub-domains

I recently registered a domain name kbcsurveyors.com. Then, I created two sub-domains, which created two new folders in the root folder.
My motive is that if I type kbcsurveyors.com/preinspection, it should point to preinspection.kbcsurveyors.com. Same for other sub domains.
In my .htaccess, which I placed inside root of mydomainname.com, I have written following lines:
RewriteEngine On
RewriteBase /
RewriteRule ^preinspection/(.*)$   https://kbcsurveyors.com/$1 [L,NC,QSA]   # Handle requests for "Preinspection"
But this file structure does not work. How do I write the .htaccess file to achieve what I want?
Regards
EDIT
I have asked a fresh question as this one has been messed up. Here is the link:How to rewrite rules for sub-domains
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub-domain1.mydomainname.com
RewriteRule ^(.*)$ http://mydomainname.com/$1 [L,NC,QSA]
If you want to redirect requests for a subdirectory to the appropriate domain, you can use the rules you have (or similar), but need to specify the scheme http:// and may use the R|redirect flag
RewriteRule ^sub-domain1 http://sub-domain1.mydomainname.com [R,NC,L]
If you also want to forward the requested path, you must capture it and use in the target
RewriteRule ^sub-domain1/(.*)$ http://sub-domain1.mydomainname.com/$1 [R,NC,L]

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

Subdomain alias with htaccess

I have this code for managing a subdomain alias: browsing sub.domain.com it shows domain.com/fp/sub.
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$
RewriteRule (.*) /fp/sub/$1
I have a lot of these subdomain to setup. Is there a way to automate this process placing some variables instead of the correct subdomain/folder name?
I tried this code with no success
RewriteCond %{HTTP_HOST} ^(sub1|sub2)\.domain\.com$
RewriteRule (.*) /fp/$1/$2
Francesco, first correcting your example, the rule should be:
RewriteRule ^(.*) fp/%1/$1 [L]
The % variables are set in the last successful cond regexp match.
Second there are lots of options if you have access to the system or vhost config, such as using Rewrite Maps or mass virtual hosts. If you don't have such access are limited to .htaccess, then you need to use one or more rules much as you are already doing. One variant is to use an existence check of the target directory, such as:
RewriteCond %{HTTP_HOST} ^(\w+)\.domain\.com$
RewriteCond %{DOCUMENT_ROOT}/fp/%1 -d
RewriteRule (.*) fp/%1/$1 [L]
Beware that if you are using a shared hosting service, that DOCUMENT_ROOT may not be properly set at rewrite execution, in which case your SHS provider will set up an environment variable to do the same, e.g. %{ENV:DOCUMENT_ROOT_REAL}. Do a phpinfo to find out. See Tips for debugging .htaccess rewrite rules for more advice on how to debug this.

.htaccess Subdomain Rewrite (not like other questions!)

I wish to rewrite admin.example.com/test/ to admin.example.com/index.php?site=test.
I have done ample research and have yet to be able to do so. Regex is not my thing, anyone have suggestions?
(in this particular example, only 'test' is a variable (i.e. 'admin' is constant'))
Thanks!
You could use mod_rewrite to do so:
RewriteEngine on
RewriteRule ^([^/]+)/$ index.php?site=$1
And if you want to restrict that rule to admin.example.com, add this condition:
RewriteEngine on
RewriteCond %{HTTP_HOST} =admin.example.com
RewriteRule ^([^/]+)/$ index.php?site=$1

Resources