.htaccess rewriterule to create subdomains - apache

I want to create subdomains, so that I can use the address test.mydomain.com.
After searching the web, and a lot of trial, I got this in my .htacces file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com(:80)?$
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/$1 -d
RewriteRule (.*) /subdomains/%1/$1 [L]
</IfModule>
If I write http: //test.mydomain.com/folder/ in my browser everything is fine.
But if I write http: //test.mydomain.com/folder (without ending slash), it gets redirected to http: //test.mydomain.dk/subdomains/test/folder/, and this is the address shown in the browser (which I don't want)
My question is:
How do I prevent the redirect?I want to only see http: //test.mydomain.com/folder in the browser. Regardless of the ending slash.
My web files is like this:
.htaccess
index.php (page for mydomain.com)
\subdomains
\test
\hello
index.php (page for test.mydomain.com/hello)
Thanks.
(sorry about the spaces in the url's. It won't let my post with links)

got it to work now:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
# If requested URI does not end in "/"
RewriteCond $1 !/$
# add a trailing slash
RewriteRule ^(.+)$ /$1/ [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com(:80)?$
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/%{REQUEST_URI} -f
RewriteRule (.*) /subdomains/%1/$1 [L]
</IfModule>

Related

.htaccess redirection to directories depends on URL

1) I have such directory structure:
.htaccess
album/
index.php
web/
.htaccess
index.php
...
...
In 'album' there is a gallery page and in 'web' there is a WordPress page.
How to fill .htaccess (I suppose in root dir) to redirect addresses depends on what is in URL:
www.domain.org -> goto /web and display wordpress page
www.domain.org/album -> goto /album and display gallery page
I've tried such thing in root .htaccess but without success (there is constant redirection to domain.org/web):
RewriteEngine On
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule ^(.*)$ /web
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org/album$ [NC]
ReWriteRule ^(.*)$ /album [L]
.htaccess in web directory is default of WordPress:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /web
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
How to solve this riddle?
Regards.
------------------
One of possible answer to thequestion (
RewriteEngine On
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule ^album(.*)$ album$1 [L]
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule web/$1 [L]
Weard is last line of this code. There are two spaces. I suppose first space means "empty string". This is strange (and also bad coding) however it works and I have enough of those redirections.
You can use these rules in root .htaccess:
RewriteEngine On
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule ^((?!album).*)$ web/$1 [L,NC]

.htaccess file redirecing incorrectly

After creating a dev site on a new server the htaccess file is redirecting links incorrectly. For example if clicking on the link example.com/test from example.com it is trying to redirect to example.com/example.com/test. How can I correct the htaccess file below:
<IfModule mod_rewrite.c>
# Turn on the Rewrite Engine
RewriteEngine On
RewriteBase /
# If the file or directory exists, show it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# Blank queries get sent to the index
RewriteRule ^$ /index.php [L]
# All other queries get sent to the index as index.php/whatever
RewriteRule ^(.*)$ /index.php?$1 [L]
</IfModule>
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
Insert this rule just below RewriteBase to redirect /example.com/test to /test:
RewriteRule ^example\.com/(.+)$ /$1 [L,NC,NE,R=302]

htaccess - canonical URL when redirecting to subdirectory

I've been playing around with this problem for quite a while but can't find the way how to achieve what I want. I want user coming to my website test.com to reach index.php located in subdirectory www immediatelly, which I am able to do. But when the URL is test.com/www I want it to be just test.com.
Snippets of code:
.htaccess in /
RewriteRule ^(.*)$ /www/$1 [L]
.htaccess in /www
RewriteCond %{REQUEST_URI} ^/www.*$
RewriteRule ^/www/(.*)$ /$1 [L]
gives me 500 Internal Error.
UPDATE:
I came to a solution thanks to #Justin Iurman's answer:
.htaccess in /www
RewriteCond %{THE_REQUEST} \ /www/?(.*)\ HTTP/ [NC]
RewriteRule ^(.*)$ /%1 [R=301,L]
Have another problem tho. On the server machine I have multiple websites and I want to serve files according to HTTP_HOST variable in root .htaccess file. I redirect user accessing test.com to proper directory (say test), but I want URL test.com/test to redirect just to test.com. Directory test contains directory www where user is redirected and it does not stick to URL thanks to solution above. I would like to edit just .htaccess file in webserver root so I don't have any dependancy on websites' domains in projects.
SOLVED
.htaccess in webserver root:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect test.com/test (and everything into test folder) to test.com root
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteCond %{THE_REQUEST} \ /test/? [NC]
RewriteRule ^.*$ / [R=301,L]
# Forward (internally rewrite) test.com/one/example/page to test.com/test/www/one/example/page
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteRule ^(.*)$ /test/www/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
.htaccess in the "test" directory:
<IfModule mod_rewrite.c>
RewriteEngine On
# allow /test in URL only for certain filetypes
RewriteRule ^(.*\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar\.gz))$ /test/www/$1 [L]
# allow /test on localhost
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteRule ^(.*)$ /test/www/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
Thanks a lot Justin!
You have to avoid infinite loop when doing what you want.
Put this code in your htaccess
RewriteEngine on
RewriteCond %{THE_REQUEST} \ /www/?(.*)\ HTTP/ [NC]
RewriteRule . /%1 [R=301,L]
RewriteRule ^(.*)$ /www/$1 [L]
EDIT: taking your update into consideration, here's the new code
RewriteEngine on
# Redirect test.com/test (and everything into test folder) to test.com root
RewriteCond %{HTTP_HOST} ^test.com$ [NC]
RewriteCond %{THE_REQUEST} \ /test/? [NC]
RewriteRule . / [R=301,L]
# Forward (internally rewrite) test.com/one/example/page to test.com/test/www/one/example/page
RewriteCond %{HTTP_HOST} ^test.com$ [NC]
RewriteRule ^(.*)$ /test/www/$1 [L]

.htaccess basic url rewrites

There are a few things I want done to the URLs of my site that I cannot seem the .htaccess file to do.
1 remove file extension f.e. example.com/file.php should be example.com/file
2 remove the www. f.e. www.example.com should be example.com (I got this part to work, but I would hate it if after I put in fix and this no longer worked
3 no one should be able to see index.php at the end of root f.e. example.com/index.php should be example.com
4 my blog page should have nice urls f.e. example.com/blog.php?article=the-name-of-article should be example.com/blog/the-name-of-article
here is my current .htaccess file
rewrite URLs
Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /
## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php
## remove www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
## remove ugly part of url for blog.php
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteRule ^blog/(.*)$ blog.php?article=$1 [QSA,L]
when I try to go to blog/the-name-of-article I get a internal server error.
From the body and comments of your .htaccess it appears that I would have provided it in the past :P
Only thing wrong in your .htaccess is ordering of rules. Always have them from most specific to most generic. Have your code like this:
Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /
## remove www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
## remove ugly part of url for blog.php
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteRule ^blog/(.*)$ /blog.php?article=$1 [QSA,L]
## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php
To remove .php:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
see Remove .php extensions with .htaccess without breaking DirectoryIndex
EDIT
For pretty URLs check this tutorial: http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

Mod Rewrite with multiple parameters

I'm trying to setup a rewrite rule so that any text in place of a subdomain will be parameter one and any text after the first forward slash will be parameter two but I'm struggling with regex and am unsure about rewrite terminology.
For example, if someone requested:
joebloggs.mydomain.com
I would like them to see:
mydomain.com/index.php?site=joebloggs
Also, if someone requested:
joebloggs.mydomain.com/contact
I would like them to see:
mydomain.com/index.php?site=joebloggs&page=contact
By "see" I mean see the page, rather than see the URL - it's a CMS-like project so you can probably see where I'm going with it. Also, I've worked out how to remove www. so that's not an issue :)
EDIT
Current .htaccess is:
RewriteEngine On
# Remove trailing slash
RewriteRule ^(.+)/$ $1 [L]
# Remove www
RewriteCond %{HTTP_HOST} ^www.richardmjenkins.com$ [NC]
RewriteRule ^(.*)$ http://richardmjenkins.com/$1 [R=301,L]
# Rewrite for site/page pair
RewriteCond %{HTTP_HOST} ^(.*)\.richardmjenkins\.com$ [NC]
RewriteCond %{REQUEST_URI} !p.php
RewriteRule ^(.+/)?([^/]*)$ p.php?s=%1&p=$2 [QSA,L,NC]
RewriteCond %{HTTP_HOST} ^(.*)\.richardmjenkins\.com$ [NC]
RewriteCond %{REQUEST_URI} !p.php
RewriteRule ^$ p.php?s=%1 [QSA,L,NC]
1. In your host panel : add a subdomain with name : * for enable all subdomains
2. this is your htaccess code :
RewriteEngine On
Options +Followsymlinks
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^$ index.php?site=%1 [QSA,L,NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^([^/]+)/?$ index.php?site=%1&page=$1 [QSA,L,NC]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.(mydomain\.com)$ [NC]
RewriteRule ^$ index.php?site=%1 [L,QSA]
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.(mydomain\.com)$ [NC]
RewriteRule ^(.+)$ index.php?site=%1&page=%2 [L,QSA]