Mod rewriting? slash to subdomain possible? - apache

can that be possible in mod rewrite? for example (mydomainname.com/myadmin) to (myadmin.mydomain.com)? how to write that in mod rewrite? so whenever the access the mydomainname.com/myadmin they get an error message of not existing.
Thanks!
--Edited---------
Sorry for that. In my website I have an admin (/myadmin) section where only moderators and administrator can access. Now there are a lot of user keep accessing it and I want to change the URL of it. Now instead of 'www.mydomain.com/MyAdmin' it would be 'MyAdmin.mydomain.com'. So whenever they go to 'www.mydomain.com/MyAdmin' they wouldn't find anything.
I just know that htaccess can do url rewriting, But I don't know how to write one.
Thank You!

Try these rules:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /myadmin[/\s] [NC]
RewriteRule ^myadmin - [NC,L,R=404]
RewriteCond %{HTTP_HOST} =myadmin.example.com [NC]
RewriteRule !^myadmin/ /absolute/filesystem/path/to/myadmin%{REQUEST_URI} [L]
That will result in a 404 response when requesting /myadmin. And myadmin.example.com will be internally rewritten to that specific myadmin directory if accessible through the file system.

Related

apache rewriterule causing redirect loop

Im trying to make an apache(2) RewriteRule which is using a QUERY_STRING to redirect a user to a friendly to read URL, but it is causing a redirect loop.
What I want to achieve is that when a user requests for the index.php?layout=751&artnr=# URL, that this request gets redirected to /another/page/#.
And in a way the Redirect works, but Apache keeps redirecting the user once it has reached the /another/page page
The RewriteRule looks like this:
RewriteCond %{REQUEST_URI} ^/index.php
RewriteCond %{QUERY_STRING} ^layout=751&artnr=(.+)$
RewriteRule ^index\.php$ another/page/%1? [R=301,L]
I've been searching alot of issues about this situation but none of them really have the answer that solves my problem, since those problems don't use a QUERY_STRING.
I have also tried to add the RewriteOptions maxredirects=1 option, but this doesn't solve the problem either.
Any help would be appreciated.
Thanks in advance.
Use THE_REQUEST variable instead of REQUEST_URI like this:
RewriteCond %{THE_REQUEST} /index\.php\?layout=751&artnr=([^\s&]*) [NC]
RewriteRule ^ another/page/%1? [R=301,L,NE]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.
This assumes a RewriteBase has been set above this rule.
Clear your browser cache or use a new browser to test this change.

having trouble writing htaccess redirects

I'm having trouble writing redirect rules in my website's htaccess file.
Basically, i want to write two rules:
1 - When i write the base URL, like http://www.example.com, i want it to automatically redirect the user to http://www.example.com/someDirectory.
2 - However, when i write http://www.example.com/Admin, i want it to redirect the user to http://www.example.com/Admin.
Here's what i've managed to do so far:
# This allows you to redirect index.html to a specific subfolder
Redirect http://www.example.pt http://www.example.pt/MainFolder
# This allows you to redirect index.html to a specific subfolder
Redirect http://www.example.pt/Admin http://www.example.pt/Admin
However this does not work... Any idea on how to do this?
Try it like this,
When there is no request for specific file or directory it will redirect you to your directory mention in rule and for the rest it will work without any rule.
Please check.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^$
RewriteRule ^ %{HTTP_HOST}/someDirectory [R,L]
After a long research i was able to find a solution to my problem. I'll leave it here, in case anyone's having the same problem:
#Rewrite everything to subfolder
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/MainFolder
RewriteCond %{REQUEST_URI} !^/Admin
Rewriterule ^(.*)$ MainFolder/$1 [L]

htaccess - passing request uri as a parameter

What I'm trying to do is as follows:
Redirect user to another address using htaccess.
Pass the given URL as a parameter to the new url
So when the user visits any page on http://domainA.com/
he should be redirected to: http://domainB.com/?referer=http://domainA.com/
What I keep failing on, is retrieving the full URL the user came from BEFORE the redirection.
One of the things I tried and failed miserably:
RewriteRule ^(.*)$ http://domainB.com/?referer=%{HTTP_HOST}%{REQUEST_URI}
Thanks in advance!
Edit:
I tried to keep my question simple, as more complex questions tend to remain unanswered,
So I will sharpen my question, although there's a good answer to the original one.
So to be more specific about what I'm trying to achieve:
Both domains are hosted on the same host, and points to exact same files.
So domainA.com/file1.html and domainB.com/file1.html will display the same file.
What I want domainA to do, is to deliver all requests to a file called listener.php.
So that all requests to domainA should be like so:
User enters http://domainA.com/file1.html
Server request behind the scenes is actually: /listener.php?actualRequest=http://domainA.com/file1.html
I want this functionality to be on the server side so that the url will remain normal.
I went for 2 domains as I wanted to avoid redirect loops.
You can use this rule:
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainA\.com$ [NC]
RewriteRule ^ http://domainB.com/?referer=http%1://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
This handles both http:// and https:// URLs.
EDIT: As per edited question you can use:
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainA\.com$ [NC]
RewriteRule !^listener\.php$ /listener.php?actualRequest=http%1://%{HTTP_HOST}%{REQUEST_URI} [L,QSA]

Redirecting all traffic based on Host name

An external server (I'll call it "sub.origin.edu") redirects all traffic to my webpage. I want to take all traffic from this host, and redirect it to a different site (which I'll call "http://foo.target.edu/board/").
My .htaccess file is:
RewriteEngine On
RewriteCond ${HTTP_HOST} sub\.origin\.edu [NC]
RewriteRule ^(.*)$ http://foo.target.edu/board/ [R=302]
This doesn't seem to be working. I've confirmed (using PHP) that the host is indeed sub.origin.edu, and the .htaccess file is in the right directory, but this rule just doesn't come into effect. Any suggestions? Thanks.
(If I remove the RewriteCond, the redirect happens, so I can confirm that everything but the rewrite condition is working.)
Use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} sub\.origin\.edu [NC]
RewriteRule ^(.*)$ http://foo.target.edu/board$1 [R=302]
You used the wrong substition character ($ instead of %)
I found this question while trying to complete a re-derict for specific hostnames.
This link was of great help to understand how RewriteCond and RewriteRule work.
http://httpd.apache.org/docs/2.4/rewrite/intro.html
If sub.origin.edu is doing a 3xx redirect, then the browser will issue a new request to your server using your.server.edu as the host name. So this rule will never match that. If this is the case, there's no easy way to tell where the request was redirected from.
If they're using a CNAME, Femi has the correct answer.

Setting subdomains using htaccess

I want to set a subdomain for all site users, like www.companyname.mydomain.com
I would like to use htaccess for this.
when somebody requests www.companyname.mydomain.com it should redirect to
myfile.php?name=companyname
How can I achieve this using an htaccess file ?
Thanks for the consideration.
Make sure this website is configured to respond to *.mydomain.com.
RewriteCond %{http_host} ^www.(\w+).mydomain.com [NC]
RewriteRule ^.*$ /myfile.php?name=%1 [L]
You might want to adjust ^.*$, since this check will rewrite regardless of what comes after www.companyname.mydomain.com.