Htaccess redirect without define hardcoded domainname - php

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/

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

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.

Get the referred after .htaccess mod redirect

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

.htaccess redirect only if GET parameter exists

I have a client with an old website without 'pretty' URLs. So currently it looks like this:
http://www.domain.com/?w=42&a=5&b=3
The parameter values are numbers only.
Now they want to move the old site to a subdomain and main (www) domain would be home to a new website (WP with SEO friendly URLs).
Now what I would like to do is redirect all requests that come to the /?w=<num> (and ONLY those) to sub.domain.com/?w=<num>, so that existing links (mostly from Google) get redirected to the subdomain page, while the new page works serving new content thorough pretty URLs.
I tried this:
# This works, but redirects the entire www.domain.com
# to sub.domain.com no mather what
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
# But this DOESN'T work
RewriteRule ^/?w(.*) http://sub.domain.com/?w$1 [R=301,L]
# Also tried to redirect 'by hand', but DIDN'T work either
Redirect 301 /?w=42 http://sub.domain.com/?w=42
What am I doing wrong? I searched high and low but always end up with this kind of suggestions. Or maybe I'm just searching for wrong keywords ...
Thank you!
You can't match against the query string inside a rewrite rule or a redirect directive. You need to match against the %{QUERY_STRING} variable. Try:
RewriteCond %{QUERY_STRING} (^|&)w=[0-9]+(&|$)
RewriteRule ^(.*)$ http://sub.domain.com/$1 [L,R=301]
Note that the query string gets automatically appended to the end of the rule's destination.
Just for documentation: If you want to redirect one directory (path) only if there is a URL parameter present, from one path to another, while maintaining the URL parameter, you can use this in your htaccess file:
# /programs/?id=1 to new path /loadprog/?id=1
RewriteCond %{REQUEST_URI} ^/programs/
RewriteCond %{QUERY_STRING} id=
RewriteRule ^programs\/$ /loadprog/$1 [R=301,L]
I am sure this will help others since I stumbled over the question above trying to find this answer.

Mod rewrite base URL

I have several domains that I want to all rewrite to one domain. I don't want it to redirect because I want the URL to look like what the user has entered in. For example if they enter www.example.com I want it to load the page from www.sample.com/default.php?from=example
I have worked a little with rewriting if you have www.site.com/var1/var2 making it load www.site.com/index.php?one=var1&two=var2
Is it possible to do what I am looking for just through the .htaccess file? I tried looking around and couldn't exactly find what I was looking for
Thanks
If the sites are hosted on different servers or don't share a common document root, then you'll have to rely on mod_proxy and you can use the P rewrite rule flag. For example, these rules in an htaccess file in www.example.com's document root:
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteRule ^/?$ http://www.sample.com/default.php?from=%1 [L,P]
Will take the request http://www.example.com/ and invisibly proxy it to http://www.sample.com/default.php?from=example. The browser's URL address bar will remain http://www.example.com/.
Note that the rule only matches against the request URI /. If you want to do more, you'd have to create the correct regular expression and grouping.
If you have redirects on the sample.com site, you'll need to employ ProxyPassReverse to rewrite the redirects. Also see ProxyPassReverseCookieDomain and ProxyPassReverseCookiePath if there are cookies involved.
If you can do this in vhost or server config instead, then consider simply using ProxyPass instead of mod_rewrite. The ProxyPass directive won't work inside htaccess files.
EDIT:
Seeing as how everything is in the same document root, you won't need to proxy anything. Simply:
RewriteCond %{HTTP_HOST} !^www\.sample\.com$ [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteRule ^/?$ /default.php?from=%1 [L]
I use online generators.
Use this generator: mod-rewrite

Categories