Rewrite subdomain but keep url - php

I have a website at domain.com, it has a login page at domain.com/user/login.
I would like login.domain.com to show the login but still have the url login.domain.com.
what i have now:
RewriteCond %{HTTP_HOST} ^login.domain.nl$ [NC]
RewriteRule (.*) http://domain.nl/user/login$1 [L]
but this changes the url as well and if i change it to:
RewriteCond %{HTTP_HOST} ^login.domain.nl$ [NC]
RewriteRule (.*) http://domain.nl/user/login$1 [P,L]
i get a 400..
what am i doing wrong?

This question is in the wrong seciton, however, you can't necessarily call the remote page like you want to (even using the proxy flag), you need to make a local reference.
You need to do something like:
RewriteCond %{HTTP_HOST} ^login.domain.nl$ [NC]
RewriteRule (.*) /user/login.php?arguments=$1 [QSA,L]
That references the local location of the file.

Related

htaccess routing with url parameters

enter code here Hi I've the following code inside my htaccess file.
My wildcard subdomain routes to "mainfolder" - here i placed the htaccess file.
I've the following folders
"mainfolder"
"mainfolder/sub1"
"mainfolder/sub2"
etc.
Calling the subdomain - sub1.domain.com it should route to the subfolder "sub1" (subfolder=subdomain).
I tried to do it with this code
#Redirect to subdomainfolder if no special page is called
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^$ %1/index.html [L]
#Redirec to subdomainfolder if a special page is called
RewriteCond %{HTTP_HOST} ^(.*)\.domain.com(.*)$ [NC]
RewriteRule ^(.*) %1/$1 [L]
The first rule works well, but if I add the second rule I receive a internal server error.
Whats wrong with this - how how I can change the first rule in this way, that it works with all url-parameters after .com - that was the reasons for me to add the second rule.
Hope I get help for this. thanks a lot.
A lot of web hosts today provide an easy implemention for subdomain creation in their administration panels. You just need to to go there, choose you subdomain name, and then point it to a directory in your tree.
If you can't, then it will be a little more complicated (You will need to resolve that subdomain to your server ip, configure some virtual hosts ... etc) and you may not have enough privileges to do that (unless you are on a dedicated server).
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
RedirectMatch 301 ^/subfolder/(.*)$ http://subdomain.example.com/$1
LIke
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^sub1/(.*)$ http://sub1.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^sub1\.example\.com$
RewriteCond %{REQUEST_URI} !^sub1/
RewriteRule ^(.*)$ /sub1/$1 [L,QSA]
If more understanding step wise then follow https://beginnersbook.com/2013/08/redirecting-from-subdirectory-to-subdomain-using-htaccess/

htaccess redirect with full url

Is it possible to redirect from one domain to another using htaccess?
RewriteRule http://www.google.com/old-route http://www.google.com/new-route [R=301,L]
RewriteRule https://www.google.com/old-route https://www.google.com/new-route [R=301,L]
If not, how would you do the redirect for multiple domains on one project?
This can be achieved by using rewriteConditions: https://wiki.apache.org/httpd/RewriteCond
Something along the lines of:
RewriteCond %{HTTP_HOST} ^site1
RewriteRule ^old-route$ https://www.site1alt.com/new-route [R=301,L]
For one Domain use.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.google\.com/old-route$ [NC]
RewriteRule ^(.*)$ http://www.google.com/new-route [R=301,L]
For redirecting everything you could use this.
Redirect 301 / http://www.google.com/new-route
Given https://www.example.com/foobar the single %{HTTP_HOST} as used in the other answers only looks for www.example.com, not taking the following path into account. So these answers were not working for me. Maybe something changed during Apache versions. What worked for me was to concatenate %{HTTP_HOST} and %{REQUEST_URI} to be checked.
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www\.example\.com/foo\.html$ [NC,OR]
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www\.example\.com/bar\.html$ [NC]
RewriteRule ^(.*)$ https://www.example.com/foobar [R=301,L]
Check out this nice Mod_Rewrite Variables Cheatsheet for an overview of many available mod_rewrite variables.

how to redirect domain name with directory

I have one project online and it on two diffrent domains 1)www.example.com and 2)www.example.co.in. now, I want to redirect
www.example.co.in/category/
to
www.example.com/ceramic+industry/
And I want to redirect it through .htaccess or from server. I tried from server that only redirect domain name not directory, and also tried from .htaccess where I can redirect only domain or only directory not when both domain and directory combine. My project in php. So, you can give advise if it's possible in php also.
Add following rule in .htaccess(placed at root of website www.example.co.in):
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^category/(.*)$ http://www.example.com/ceramic+industry/$1 [L,R=301]
or use 302 for temporary redirects.
IF you also want to redirect whole .co.in website to .com THEN add next line(remember in .com website's .htaccess these two Rules should not be there.)
RewriteRule ^category/(.*)$ http://www.example.com/ceramic+industry/$1 [L,R=301]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
If for any reason you are bound to use same .htaccess on both site then use condition like this:
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^category/(.*)$ http://www.example.com/ceramic+industry/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
============================================================
Solution to your another query:
This can be done by PHP. As I guessed all URL after category/ is handled by one PHP page.
in that PHP page code this at top:
if(isset($_GET['company'])){
$company=$_GET['company'];
$companyNew=str_replace('_','+',$company);
if($company!=$companyNew){
header("location:/category/?company="+$companyNew,true,301);
//header("location:/ceramic+industry/?company="+$companyNew,true,301);
exit;
}
}
You can try this:
RewriteCond %{HTTP_HOST} ^your.first.domain$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://your.second.domain/$1 [R,L]
This will redirect, if your request contains existing directory name.
Answer to your comment. You can try this:
RewriteCond %{ENV:REDIRECT_FINISH} !^$
RewriteRule ^ - [L]
RewriteCond %{QUERY_STRING} ^company=(.*)\_(.*)$
RewriteRule ^(.*)$ /$1?company=%1+%2 [R,L,E=FINISH:1]
First two lines allow you to prevent infinite loop redirect.

Rewrite Subdomain to query string and mask. Currently looping and 403'ing

It's been a while!
Hi guys,
So I'm trying to achieve the following:
www.domain.com
domain.com -> 301 -> www.domain.com
account-name.domain.com -> mask and ask server for -> domain.com/account/acount-name
account-name.domain.com/page -> mask and ask server for -> domain.com/account/account-name/page
etc..
So far I have got this in my htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^(.*)$ account/%1$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
However I keep getting this sort of 403 message, it looks like a loop!
You don't have permission to access
/index.php/account/testindex.php/account/testindex.php/account/testindex.php/account/testindex.php/account/test
Maybe I'm missing something obvious as I've been working on this for a while and gave up last time I tried. Is it even possible?
My next challenge is...
Once I get this sorted I am planning on allowing users to setup CNAMES and point to our domain.com, I want to be able to rewrite the non domain.com requests to something similar as above in structure if not the same because we can obviously test for the domain format using regex to then load the account from the DB. So ANY suggestions on this would be a bonus :)
Setup
Currently using Codeigniter on Apache and Ubuntu 14 Server.
Thanks for any guidance in advanced!
-Stefan
Try changing
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
to
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
The L ensures that this rewrite is the last processing rule for this request. Otherwise Apache will also apply your other rewrite rules as well, leading to this:
/index.php/account/testindex.php/account/testindex.php/account/testindex.php/account/testindex.php/account/test

htaccess subdomain rewrite to variable without being visible in the url

I need to convert "subdomains" to variables using htaccess.
But I don't want the original url to be displayd after the rewrite.
Here's what I have so far:
rewritecond %{http_host} ^([A-Za-z0-9-]+)\.mysite.\com$ [nc]
rewriterule ^(.*)$ http://www.mysite.com/$1?aff=%1 [nc]
But it doesn't seem to work.
What I need is the following:
User links to:
aff1.mysite.com
The displayed url changes to:
www.mysite.com/
But the actual url that the server reads is:
www.mysite.com/?aff=aff1
I'm also going to be adding languages to the mix, so I might even need something like the following:
User links to: aff1.mysite.com
Then I get the language from a cookie {HTTP_COOKIE} lang=([A-Za-z]+)$
If there's nothing in the cookie, the default should be "en"
Then the user gets redirected to aff1.en.mysite.com
Which should then be displayed as: www.mysite.com/en/
but to the server: www.mysite.com/?lang=en&aff=aff1
Please tell me this is possible, and how I can achieve this. I'm very new to rewriting. And I've viewed other questions and tried their solutions, but somehow can't adapt it to exactly what I need.
Also... www.mysite.com shouldn't go to www.mysite.com/aff=www
It should just go to /mysite.com/en/
So, I've spent way more time than I wanted to on this, but finally got something working. Instead of passing the variables in the url, I'm dropping a cookie (which was ultimately what I was doing on the page anyway, but this cuts out a step - and I didn't know that you could drop cookies with the rewrite).
If anyone has a better solution with less lines of code, please feel free to comment or post a new answer.
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{http_host} ^www.example.info [nc] #if url starts with www.example.info
RewriteRule ^(.*)$ http://example.info/$1 [r,nc] #rewrite without the www
RewriteCond %{http_host} ^www.(.*).example.info [nc] #if url starts with www.(aff).example.info
RewriteRule ^(.*)$ http://%1.example.info/$1 [r,nc] #rewrite without the www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{http_host} ^(.*)\.example.info [nc] #if subdomain is aff
RewriteRule ^(.*)$ - [co=aff:%1:example.info:7200:/] #drop/update cookie with aff
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{http_host} ^(.*)\.example.info [nc] #if subdomain is aff
RewriteRule ^(.*)$ http://example.info/$1 [nc,R,L] #rewrite without subdomain

Categories