htaccess redirect for global subdomains system - all subdomains redirected to one php file to handle them all - apache

i really could use some advices related to subdomains.
recently we decide to make every subdomain of main portal to be available for blogs (something similar to lets say any blog platform)
so what i cant achieve is to make redirection to certain folder of main portal site or php file like:
xxxxx.domain.com/index.php?get=xx&get2=xx -> to be run by actual php file located on main portal account, lets say subdomains.php?get=x&get2=xx, alternatively - to be run by mainportal/subdomains/index.php?get=x&get2=xx
i tried with
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.pl$ [NC]
RewriteRule (.*) subdomain.php [L]
effect was almost correct but get parameters are missing:(
on the other hand it can be entirely wrong... i simply dont understand or that rewrite rules:(
thanks for any help/tips

You can try with QSA (query string append):
RewriteCond %{HTTP_HOST} ^.*\.domain\.pl$ [NC]
RewriteRule . subdomain.php [QSA]

Related

Internally rewrite a subdomain to main domain

I am trying to internally rewrite URLs of a subdomain:
e.g. sub1.domain.com/something/ silently goes to to main domain domain.com/sub1/something/.
I have been able to get this to work properly as a redirect. However, I do not want them to redirect.
I still want it to show sub1.domain.com/something/ as the URL and simply display the content from domain.com/sub1/something/.
Need some help achieving this. Here's what I have so far:
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com [NC]
RewriteRule (.*) http://domain.com/sub1/$1 [L]
Note: both domains are pointing to the same server (and i can make them share the same document root folder, if needed)

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]

301 Redirect Whole Site Except One Folder, leave Add-on domains alone

I have a brainteaser and need help from people smarter than me. I have a shared hosting account. I'd like to 301 forward the root URL (say, domain.org) to a new URL. I also want one folder (/blog/) to be left alone (not forwarded). I was able to find an example of this here, and I put together this potential scenario for doing that:
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ http://newdomain.org/$1 [L,R=301]
I believe that this should be OK, but here's the trick: I have add-on domains in this hosting, and if I use the above, I'm pretty sure that I will forward every one of them to newdomain.org, not just domain.org. I did some testing using more specific text strings in the first spot following RewriteRule, but I can't seem to get the syntax without blowing up my site and getting a 500.
Any ideas would be greatly appreciated!
Thanks, Dave
Try adding another condition:
RewriteCond %{HTTP_HOST} ^domain\.org$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ http://newdomain.org/$1 [L,R=301]
Where domain.org is the domain that you want everything to be redirected to newdomain.org, except /blog/.

Subdomains for redirection only

My domain is meeting.com.
I work with PHP, jQuery, for the main part. No ASP.
When a user creates an account, I'd like a subdomain user.meeting.com to be created for REDIRECTION purposes only. I don't want the subdomain to exist for real.
When someone would load john13.meeting.com, I would like the website to redirect to meeting.com?user=john13 (I don't care if john13.meeting.com remains in the URL bar).
What is the most efficient and easy method of doing it automatically in PHP or Apache? Please consider performance too since I have many users.
You don't need PHP for that. The right Apache configuration with the rewrite module on, will do the trick.
For example (not tested):
RewriteCond %{HTTP_HOST} !^www\.meeting\.com?$
RewriteCond %{HTTP_HOST} ^([^.]+)\.meeting\.com?$
RewriteRule ^$ /user/%1 [L]
Or if you're not rewriting further for SEO, the rule could be
RewriteRule ^$ /index.php?user=%1 [L]
For more inspiration, check out the link Shef posted with another example of how to achieve this.

Using Apache mod_rewrite for conditional redirection if URL exists

I'm migrating a large site (with significant changes to each page), and want a test group of users to be automatically redirected from the current site to the new site as each page is completed.
What was
www.mysite.com/admin/somefile.php
will become
admin.mysite.com/somefile.php
As each page is migrated to the new site, I want users from a specific IP address to be automatically redirected to it. This looks like a case for the '-U' (existing URL) flag of RewriteCond. The mod_rewrite statements (for www.mysite.com) would be something along the lines of
RewriteEngine On
RewriteCond %{REMOTE_ADDR} 123\.123\.123\.123 # for test users only
RewriteCond http://admin.mysite.com/$1 -U # if new page exists
RewriteRule /admin/(.*) http://admin.mysite.com/$1 [R=302,L] # then redirect
However, this doesn't seem to work - perhaps because the $1 parameter is being referenced before being defined?
Can anyone advise how this can be achieved?
Thanks, Chris
Thanks, LazyOne – you gave me confidence to pursue it (if you make it an answer I'll accept it).
I think the problem was simply in using /admin instead of ^admin; my final mod_rewrite statements are:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} 123\.123\.123\.123 # for test users only
RewriteCond http://admin.mysite.com/$1 -U # if new page exists
RewriteRule ^admin/(.*) http://admin.mysite.com/$1 [R=302,L] # then redirect
...which seems to correctly serve the page from the new site if it exists, otherwise serves the page from the old site.
You could use it this way to manipulate URI
RewriteCond %{REQUEST_URI} ^/admin/(.*)$
RewriteRule .* http://admin.mysite.com/#1 [R=302,L]
Hope this works. BTW, you can test your rewrite online here http://martinmelin.se/rewrite-rule-tester/