Get the referred after .htaccess mod redirect - php

I am redirecting users from wildcard sub-domains to a specific sub directory using mod rewrite rule while not changing address bar url. I have done it success fully but I also need the wildcard sub-domain name to use internally.
For example if someone try xyz.domain.com I am redirecting him to say domain.com/abc and I also need that xyz for internal use.
I tried $_SERVER['SERVER_NAME'] but it returns current sub directory address. Also tried $_SERVER['HTTP_REFERER'] but with no success. So if anyone can help please.
My .htaccess code is given below:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^(.*)$ "http\:\/\/domain\.com\/project\/" [P,NC,QSA]

My previous answer doesn't work with dynamic subdomains. So in your case it wont work.
When I tested your .htaccess rules, my server sets a $_SERVER['HTTP_X_FORWARDED_HOST'] variable with a value of original host "xyz.domain.com".
Maybe this could help.
Answer for static spesific subdomains;
put these lines to .htaccess before your redirect condition
RewriteCond %{HTTP_HOST} ^xyz.domain.com(.*)$ [NC]
RewriteRule .* - [E=internalSubdomain:xyz]
then you can use $_SERVER['internalSubdomain'] and $_SERVER['REDIRECT_internalSubdomain'] in your php code

Related

Permanent redirect htaccess to a new domaine keeping a part of the URL as it is

I never wrote any htaccess codnitions and the one by default dont give me enough to work with to get what I need,
I have a URL like : http://domaineA.com/target:/to/be/removed/keep/this.ext
And I want to redirect all URLs like this one to :
http://domaineB.com/keep/this.ext
I tried this, but didn't work:
//301 Redirect Entire Directory
RedirectMatch 301 http://domaineA.com/target:/to/be/removed(.*) http://domaineB.com/$1
Update:
I forgot to mention that the /keep/this.ext is dynamic, it represents all files of a my directory.
Thank you
You can use this rule as your very first rule in site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domaineA\.com$ [NC]
RewriteRule ^/?target:/to/be/removed(/.*)?$ http://domaineB.com$1 [L,NE,R=301,NC]
you can put this in your .htacces or your virtualhost as below
Redirect permanent http://domaineA.com/target:/to/be/removed/keep/this.ext http://domaineB.com/keep/this.ext

Htaccess redirect without define hardcoded domainname

I find a lot lot lot questions about redirection with htaccess.
However it seems no one has the same "problem" as me.
What I want is redirect if a directory is accessed, it should redirect to the subdomain. So far its good and working. However, I dont want to change my htaccess all the time when a domainname is changed.
So I have:
RedirectMatch 301 ^/subdir/(.*)$ http://subdir.example.com/$1
But in my case I put my application on the domain example2.com, I have to change the name.
Is it possible to have something like this:
RedirectMatch 301 ^**(capture_hostname)**/subdir/(.*)$ http://subdir.**(put_hostname_here)**/$1
All "solutions" seems to work with defining the domainname in htaccess.
I work not enough with htaccess to solve this issue. Therefore we are together :)
You can do it with mod_rewrite rules and capture hostname from a RewriteCond:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(subdir)(/.*)?$ http://$1.%1$2 [L,NC,R=301,NE]
%1 is the value being captured from RewriteCond directive, $1 is subdir and $2 is part after subdir/

How do I route all subdomains to index.php using htaccess without 301 redirect

I would like to set up an htaccess file that routes requests for any subdomain to the index.php file in the /public_html/ folder. I have already configured the DNS to accept wildcard subdomains.
I plan to give users a personalised url for the site and pass the username via the subdomain, so it is important that the url remains and hence 301 redirects are out of the question.
My htaccess currently looks like this, but does not work (server not found):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule . /index.php?subdomain=%1 [L,QSA]
My end goal is to be able to get a url such as //joebloggs.mydomain.com/foo/bar to translate into something like //www.mydomain.com/index.php?user=joebloggs&route=/foo/bar.
What do I need to have in the htaccess file to make this work?
You can have it this way
# if request is for a subdomain (except "www")
RewriteCond %{HTTP_HOST} ^((?!www\.).+)\.mydomain\.com$ [NC]
# internally rewrite to index.php
RewriteRule ^((?!index\.php$).*)$ /index.php?subdomain=%1&route=$1 [L]
Since it seems the rule is executed when you're trying to navigate to the index.php, can't this be done with simple php logic within index.php?
I mightve misunderstood your goal, but it seems to me you could easily replace this with a GET. Your url would be ready for that approach, too.

Replace part of filename in URI with htaccess mod_rewrite x-domain solution required

I have been trawling the tutorials on htaccess but every example seems to have an absolute URL in the second part of the rewrite. My site delivers across several domains so I need a x-domain solution.
Rule should state the following: Everything that did go to the confirm.php file now goes to confirm/ folder. So...
http://example.com/confirm.php?token=32879 should now go to http://example.com/confirm/?token=32879
AND
http://elpmaxe.com/confirm.php?token=32879 should now go to http://elpmaxe.com/confirm/?token=32879
The code I have come up with is:
RewriteCond %{REQUEST_URI} confirm.php$ [NC]
RewriteRule ^(confirm\.php)$ confirm/ [L,R=301]
But this appends the entire document root path to the end of the URL as follows:
http://example.local/var/www/vhosts/example.co.uk/confirm/?t=CB3Qj&e=578
Please help!
This rule should work:
RewriteEngine On
RewriteRule ^(confirm\.php)$ /confirm/ [L,R=301,NC]
Make sure to test in a new browser to avoid 301 caching issues.

Www and non www sites

I have a domain say http://www.testexample.com. When I login to http://www.testexample.com and come back to http://testexample.com in browser; the logged in user information is not displayed.
I know that the both of the above are treated differently and hence it is not retaining the session for http://www.testexample.com while accessing http://testexample.com.
Please let me know if cakephp has a way to do a match on the TLD. So whenever I type http://testexample.com it should take session for http://www.testexample.com
I am using the following code to redirect from one URL to the other
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ps6309 [NC]
RewriteRule ps6309.domain.co.in [L,R=301]
this is on my local test machine. This works sometimes and sometimes doesn't.
Also I have added the rewritelog directive to my httpd.conf file.
But the log file is not getting updated.
Please let me know if anyone has any pointers to this.
Use .htaccess to redirect all http://domain.com -> http://www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
Set the domain for the cookie as testexample.com, then it can be shared across sub domains as well as not worrying about www.
If you have many projects and don't want to hard code your domain name on .htaccess again and over again, try this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
this will redirect non-www to www. While you're using cakephp, then put it on your .htaccess which is located at /webroot
Try ini_set('session.cookie_domain', $domain); (documented as ini_set session.cookie_domain and session_set_cookie_params()), where $domain is your domain name prefixed by a .. So, using the domain example.com (per rfc 2606), you'd use:
ini_set('session.cookie_domain', '.example.com');
Please note that this is not a CakePHP specific solution - looking at the code for CakeSession, session.cookie_domain is never set, meaning that it falls to it's default value. Stuffing that line in your app/config/bootstrap.php or app/config/core.php should do it for you.

Categories