.htaccess Rewrite Rules for subdomain - apache

I use codeigniter as my main install on the main domain. I have created a subdomain and a folder called live e.g. live.domain.com maps to public/live . However in public I use codeigniter.
I now have the dynamic codeigniter url:
http://domain.com/api/
which I want to map to my subdomain:
https://live.domain.com
So going to:
https://live.domain.com/api/functioname
would be using the script:
http://domain.com/api/apifunctioname
and possibly:
http://domain.com/api/apifunctioname/parameter1/parameter
Everything is on the same server so no redirects are needed.
Anyone have any ideas on which rewrite rules to use?
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^live\.domain\.com [NC]
RewriteRule (.+)$ "http://domain.com/api/$1" [L]
The above works great as a rewrite but redirects to http://domain.com/api/functionname instead I want it to route; so that when going to:
https://live.domain.com/api/functioname
It stays at that url but uses the script of
http://domain.com/api/functionname
Thank you very much,
Ice

How about something like the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^live\.domain\.com$ [NC]
RewriteRule (.+)$ "https://domain.com/api/$1" [L,P]

Just add index.php, see below
RewriteEngine On
RewriteCond %{HTTP_HOST} ^live\.domain\.com$ [NC]
RewriteRule (.+)$ "https://domain.com/index.php/api/$1" [L,P]

Related

.htaccess issue with using a -org secondary domain vs using a .com secondary domain

I have successfully forwarded www.atlantalawvideo.legalvideoconsulting.com to www.atlantalawvideo.com for SEO purposes.
I'm using this code in my .htaccess file in my /atlantalawvideo/ folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.atlantalawvideo.com
RewriteRule (.*) http://www.atlantalawvideo.com/$1 [R=301,L]
I'm having trouble forwarding www.legalvideographer-org.legalvideoconsulting.com to www.legalvideographer.org
I'm using this code in my .htaccess file in my /legalvideographer-org/ folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.legalvideographer.org
RewriteRule (.*) http://www.legalvideographer.org/$1 [R=301,L]
Not sure what the problem is http://www.legalvideographer-org.legalvideoconsulting.com forward goes nowhere.
The second rewrite is not working because you are not meeting the condition of the %{HTTP_HOST}. You're not using the full host name to check. legalvideographer.org appears to be a subdomain oflegalvideoconsulting.com. See if this works for you.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.legalvideographer-org\.legalvideoconsulting\.com [NC]
RewriteRule (.*) http://www.legalvideographer.org/$1 [R=301,L]
That should work if that is your domain name.
You should also be able to do a simple redirect instead of the rewrite rule
redirct 301 / http://www.legalvideographer.org

How to create mode_rewrite rule on Apache Server FreeBSD 8.1

I'm Working on a server running apache version 2.2.25 on FreeBSD 8.1.
Lets say I have the the following url: this.domain.com/html/folder/index.php
And I want to rewrite it as this: this.domain.com/index
How specifically would I create the rewrite rule? Should I create the rewrite rule in the httpd.conf file or the .htaccess file, or does it not really matter?
Also, is there a way to change the domain name "this.domain.com" to something else?
Assuming, you want users to enter the shorter url this.domain.com/index and serve the php at this.domain.com/html/folder/index.php; add the following rules to the .htaccess at root /.
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^this\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ html/folder/$1.php [L]
I'm not sure what you mean by changing your domain to something else. this.domain.com can be changed to that.domain.com or addon-domain.com but would make sense only if their DNS point to the same server (to serve shared content). Otherwise, it's like an external redirect to some other site.
RewriteCond %{HTTP_HOST} ^this\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://that.domain.com/$1 [R=301,L]
Try this
RewriteRule ^/index$ /html/folder/index.php [PT]

apache mod rewrite for subdomain

On my webserver, I have one website located at example.com/site (in /var/www/site).
I want it to be accessed through site.example.com, and I'm aware I need to use mod-rewrite to enable this.
Is there a concise snippet I can use to do this? I need to make it extensible so other sites can be accessed this way as well. Thanks in advance.
If this is a matter of one specific subdomain: site, then you should explicitly rewrite just that one subdomain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule ^(.*)$ /site/$1 [L]
The second condition prevents an internal rewrite loop and making your URI look like /site/site/site/site/site/site/ etc...
This would go in your .htaccess file in your document root, /var/www/
If you really want to arbitrarily redirect any subdomain, you can try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]*)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)[^:]*:\1 [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
Here, the second condition serves the same purpose, it tries to make sure the request URI doesn't start with the subdomain match from the host. See https://stackoverflow.com/a/11508430/851273 for a better explanation.
First you need to configure your server to accept any subdomain for your domain example.com and redirect it to your virtual host that as well has to accept any subdomain. After that, you can use the following rule to rewrite that subdomain internally to a folder with the same name:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ %1%{REQUEST_URI} [L]

Using .htaccess to redirect domain.co.uk/index.html to www.domain.co.uk

I have noticed that search engines have been crawling both the domain.co.uk and www.domain.co.uk versions of a web site that I've recently developed.
Using .htaccess I have been able to setup http 301 redirects so that:
http://domain.co.uk is redirected to http://www.domain.co.uk
and
http://www.domain.co.uk/index.html is redirected to http://www.domain.co.uk
However:
http://domain.co.uk/index.html does not get redirected to http://www.domain.co.uk as I would expect. Instead the redirect goes to: http://www.domain.co.uk/http://www.domain.co.uk/
The contents of my .htaccess are (obviously I have replaced my domain to simplify matters):
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^domain.co.uk [nc]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,nc]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.domain.co.uk/$1 [R=301,L]
I know I am probably missing something obvious but please could someone help me get the final redirect to http://www.domain.co.uk working as expected?
The first part of your rule set is working fine, the missing www. is added correctly. For the second part you only need a simple rule to remove the index.html without using any additional condition:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,NC]
RewriteRule ^index.html$ http://www.domain.co.uk/ [R=301,L]
I think this should do the trick.

How do I use mod_rewrite to alter which file is fetched based on the URL's sub-domain?

So say I have a URL being requested:
sub.domain/file.bin
and I wanted this file to be fetched:
domain/hosted/sub/file.bin
How would I structure the rewrite directives? I've been working on this for days, and it just gives me a headache...
The sub-domains are numerous and are added and removed often, so I'm trying to find a solution that works for all of them.
In sub.domain's httpd config:
RewriteCond %{HTTP_HOST} ([^\.]+).domain
RewriteRule ^/file.bin$ http://domain/hosted/%1/file.bin
Assuming the home directory for www.domain.com is domain/ and that - given any subdomain sub.domain.com - you want the files for the home directories for that sub-domain to be in domain/hosted/sub/
Try something like this:
RewriteEngine on
// If the host is just mydomain.com, do nothing more
// this is to prevent some recursion problems I've read of...
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^.*$ - [L]
// Otherwise strip off everything before mydomain
// And add it to the start of the request
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?mydomain\.com$ [NC]
RewriteRule ^.*$ %1%{REQUEST_URI} [QSA]
// Then prefix with 'hosted'
RewriteRule ^(.*)$ hosted/$1 [QSA,L]
You may also need a wildcard entry in your DNS or something... but I will admit DNS and htaccess mod rewrite are some of my weaker points. See also http://www.webmasterworld.com/forum92/138.htm
Try this rule:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %1 !=www
RewriteRule !^hosted/ hosted/%1%{REQUEST_URI} [L]