confused about .htaccess subdomain rewrite (not redirect) - regex

I have 2 domains pointing to the root of my site:
http://site1.com
http://site2.com
both point to http://dXXXXXXX.domain.com/
I have each site set up to look for its files in a subdirectory (of the same name) so
http://site1.com files are located in http://dXXXXXXX.domain.com/site1/
http://site2.com files are located in http://dXXXXXXX.domain.com/site2/
I would like to create a special subdomain for site1 (and possibly site2) that actually points to a folder in the main directory (a forum that both domains can share). I would like to mask the real url with the subdomain url like the following:
http://subdomain.site1.com masks http://dXXXXXXX.domain.com/shared_folder/ (not redirected)
my .htaccess file (in the topmost directory) looks like this:
Options +FollowSymLinks
RewriteEngine On
# Rewrite "www.domain.com -> domain.com"
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
# redirect site1.com to /site1 [folder]
# redirect site2.com to /site2 [folder]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^0-9]+)$ [NC]
RewriteRule ^(.*)$ /%1/$1 [QSA,L]
#i think this should work but it doesn't
#RewriteCond %{HTTP_HOST} ^subdomain\.site1\.com$ [NC]
#RewriteRule ^$ shared_folder [P]
also, if possible, I would like to redirect the absolute path to each domain to my preferred path as follows:
http://dXXXXXXX.domain.com/site1/ redirects to http://site1.com
http://dXXXXXXX.domain.com/site2/ redirects to http://site2.com
not sure that anyone would ever find those links, but I'd like to keep everything clean by not having multiple access points for the same piece of content (with the exception of intentionally shared css or forum software)
Thanks in advance for any help or insight you may have for me

I think I figured it out (as in it hasn't broken yet!)
Options +FollowSymLinks
RewriteEngine On
# Rewrite/Redirect "www.domain.com -> domain.com"
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Rewrite/Redirect "domain.com/foo -> domain.com/foo/"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ /$1/ [R=301,L]
# Redirect/Pass through "subdomain.site1.com to /shared_folder/"
RewriteCond %{HTTP_HOST} ^subdomain\.([\w.]+)$ [NC]
RewriteRule ^(.*)$ /shared_folder/ [L]
# Redirect/Pass through site1.com to /site1 [folder]
# Redirect/Pass through site2.com to /site2 [folder]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^0-9]+)$ [NC]
RewriteRule ^(.*)$ /%1/$1 [QSA,L]
So this works, but I'm not sure about the placement of my rules: if it's better to tackle the special subdomain case before the domains themselves, as in the order I have now; or if RewriteRule ^(.*)$ /shared_folder/ [L]is better written as RewriteRule ^$ /shared_folder/ [L]...
If you have any insight as to good or better practices I would appreciate it, thanks.

Related

Redirecting naked domain to www. in a subfolder

I am hosting multiple domains on one hosting account, so I have made folders in the public_html folder for each domain.
e.g. home/username/public_html/examplewebsite.com/
and
home/username/public_html/otherexamplewebsite.com/
I've been trying to figure out the code I need in my htaccess file to redirect visitors to the appropriate subfolder when they type in examplewebsite.com so that they don't see that they're in a subfolder of the main domain. I found this code originally, which was working:
Options -Indexes +SymLinksIfOwnerMatch
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/domainfolder/
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.
#Rewrite all those to insert /folder
RewriteRule ^(.*)$ /domainfolder/$1 [L]
but I also want to redirect all visitors who type in the naked domain to the www. version. I found this code, but when I use them both in the htaccess file you can see the subfolder name in the url again:
#Only www, no naked domain
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
I don't really understand much of all this code so If any of you gurus could help me figure out a solution I would really appreciate it!
Keep your rules like this in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\. [NC]
RewriteRule !^domainfolder/ domainfolder%{REQUEST_URI} [L,NC]

How to redirect root to subdirectory using .htaccess

I am currently using shared hosting and have no way to put my files outside the "public_html" folder.
To work around this, I want to rewrite my "root" site to a subdirectory using the script below in an .htaccess file:
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteRule ^(.*)$ /subdir/$1
Which works great, except when I navigate to www.example.com/subdir/. That url does not redirect to anywhere, it simply displays the subdirectory. This is a problem for relative urls and such.
Is there a way to continue rewriting my root to a subdirectory while also rewriting www.example.com/subdir/ to www.example.com?
You can use this rule in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteCond %{REQUEST_URI} !^/subdir/ [NC]
RewriteRule ^(.*)$ subdir/$1 [L]
And use this rule in /subdir/.htaccess:
RewriteEngine On
RewriteBase /subdir/
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteCond %{THE_REQUEST} /subdir/(\S*)\s
RewriteRule ^ /%1? [R=302,L,NE]

.htaccess redirect domain to subdirectory without changing URL

I have the following setup:
-> domain1.com is my main domain and also document root
-> domain2.com is another domain which is registered as an alias domain (means it does exactly the same as domain1.com), I have multiple of those domains.
when I now want to redirect that domain to something I usually use this to redirect the domain to something else (like an external website):
RewriteCond %{HTTP_HOST} ^(www\.)?domain2.com$ [NC]
RewriteRule ^(.*) http://somewiredisp.com/user/bla/$1 [L,R]
this is usually used if I register a domain for someone who needs a nice url for their forums or whatever. Works like a charm - the only "problem" would be that the address bar in the browser changes, but I've read that there would be no way to do that with external URLs, and since no one ever complained about it I am fine with it.
However, now I would like to link some url to a subdirectory WITHOUT the url changing in the address bar.
domain2.com -> domain1.com/subdir (or domain2.com/subdir - that doesn't matter, since it's an alias domain).
my current approach would be
RewriteCond %{HTTP_HOST} ^(www\.)?domain2.com$ [NC]
RewriteRule ^(.*) http://domain1.com/subdir/ [P]
which doesn't work (Error 404) - if I call domain1.com/subdir/ directly if works obviously.
I also tried several variations of
RewriteRule ^(.*) http://domain2.com/subdir/ [P]
RewriteRule ^(.*) /subdir/ [P]
RewriteRule ^(.*) http://domain1.com/subdir/index.html [P]
all with the same result.
maybe it's just a simple mistake but I am currently clueless :/
You can use this rule:
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule !^subdir/ /subdir%{REQUEST_URI} [L,NC]
This will prefix every request for domain2.com with /subdir if it is not already there.
Below code works perfectly for me.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/folder_name/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder_name/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ folder_name/index.html [L]
Show subdirectory pages without changing url in root domain
Change according to your need subfolder_name in code
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteRule ^(.*)$ /subfolder_name/$1
</IfModule>

rewrite rule in htaccess

I have a website structured:
- root
- .htaccess
- wordpress installation
- folder xxx
- wordpress
In my .htaccess I have this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The problem is that it don't allow me to go in:
www.mywebsite.it/xxx
because a loop redirection happen.
If I erase the rule, all goes good.
How can I modify it?
I want that if user type mywebsite.it/ or mywebsite.it/xxx, it is redirected to www version.
my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Thanks a lot.
This rule works in isolation but Wordpress stuff is hijacking over it. Slightly modified rule:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Make sure this is 1st rule after RewriteEngine On line in DOCUMENT_ROOT/.htaccess
make sure in WP permalink structure you have site address with www. otherwise WP will again send it back to non-www
If you have any .htaccess below root then they should this additional line below RewriteEngine On line:
RewriteOptions Inherit
Also test in a different browser to avoid 301 caching.

Remove 'index.php' from URL with .htaccess

I've been trying all sorts of solutions from this site and none seem to work. I'm currently hosting with hostgator. This is my current .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule mod_suphp.c>
suPHP_ConfigPath /home/user/php.ini
<Files php.ini>
order allow,deny
deny from all
</Files>
</IfModule>
This is in the root folder of my site. I have also tried adding a ? after index.php and no luck. Does anyone know why this isn't working?
This is the code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
Symfony 2 has an excellent solution:
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/index.php [L]
This accomplishes the following:
RewriteBase is not necessary (useful when the site is in a subdirectory beneath the web root)
index.php is removed if present
The request is routed to the correct index.php with the full query string from the original request
Note that the line:
RewriteRule ^(.*) - [E=BASE:%1]
is responsible for setting the %{ENV:BASE} variable for later on. Refer to Apache documentation on E|env flag.
I tried this and works fine:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Exceptions
If your site’s system directory (/system/) has been renamed and is still accessible by URL, modify the RewriteCond line above:
RewriteCond %{REQUEST_URI} !/newdirectoryname/.* [NC]
If you are running EE from a sub-directory rather from the root of your domain (e.g. http://example.com/myeesite/ instead of http://example.com/), just remove the slash preceding index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ index.php/$1 [L]
If you are running EE from a sub-directory and it still doesn’t work after removing the slash, you may need to specify the sub-directory in your rewrite rule. For example, if your sub-folder is named testing, change:
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
To:
RewriteRule (.*?)index\.php/*(.*) testing/$1$2 [R=301,NE,L]
And change:
RewriteRule ^(.*)$ /index.php/$1 [L]
To:
RewriteRule ^(.*)$ testing/index.php/$1 [L]
If your host requires forcing query strings, try adding a question mark following index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ /index.php?/$1 [L]
To remove index.php from urls on apache2.4 you can use the following rule :
RewriteEngine on
RewriteRule ^index\.php/(.+)$ /$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !index\.php /index.php%{REQUEST_URI} [L,END]
This will change the uri
/index.php/foobar
to
/foobar
This rule will return an internal server error for lower versions of apache as they don't support the END flag. Please see the anubhava's answer above that works almost on all versions.
I've been compelled to join stack overflow.com today to comment here as the answer has solved a long term problem I've had with a Zend Framework website. I've worked on the website around 3 1/2 years and during that time I discovered that it didn't handle index.php correctly and this causes webmaster tools to see duplicate titles etc.
I decided to search again today for a solution, one of many times attempted.
This is the one (the last answer shown above) that solves my problem.
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
As an example. this is what it achieves for me.
http://www.domainname.co.uk/index.php/categories/url-name.html
becomes
http://www.domainname.co.uk/categories/url-name.html
Thank you to everyone who contributed to the original question as it lead to the answer and solution above.
Extra Note: I have other rewrite commands that handles the other aspects but those on their own didn't fix the index.php and the only time this has been fixed is by using.
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
I only hope it helps others whether its ExpressionEngine or Zend Framework, in the future.