There's tons of resources online about using .htaccess to rewrite your subdomains and if need be, also rewrite your main domain to a subfolder. I have found plenty answers and most of them are exactly the same. I have been tediously testing these methods and I have the same problem in all cases.
Consider the wanted result:
maindomain.com : rewrite to /public_html/mainsite/
sub.maindomain.com : rewrite to /public_html/sub/
The fastest/cleanest way i have considered is the following:
RewriteEngine On
# Rewrite the main domain
RewriteCond %{HTTP_HOST} !sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/mainsite
RewriteRule ^(.*)$ /mainsite/$1 [L]
# Rewrite the sub domain
RewriteCond %{HTTP_HOST} sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/sub
RewriteRule ^(.*)$ /sub/$1 [L]
This works well except for 1 annoying issue; The line
RewriteCond %{REQUEST_URI} !^/mainsite
Basically prevents a rewrite loop, but if you browse to maindomain.com/mainsite/ it rewrites to /public_html/mainsite/ instead of /public_html/mainsite/mainsite/ hoping to raise a 404 not found. If i remove that line, i get a 500 server error as it goes into a loop :S
The issue is, that any one of these domains needs freedom of creating folders etc. and would like to ensure that there is absolute freedom in the sub-sub folders people create :S
Please could someone help here?
RewriteEngine On
# Rewrite the main domain
RewriteCond %{HTTP_HOST} !sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/mainsite/.*
RewriteRule ^(.*)$ /mainsite/$1 [L]
# Rewrite the sub domain
RewriteCond %{HTTP_HOST} sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/sub/.*
RewriteRule ^(.*)$ /sub/$1 [L]
You need to make it so that it does not match any file within the /mainsite directory not just the root (/mainsite). I would think you would need to do the same to the sub domain also.
actually try this if you are still looking for an answer
RewriteEngine On
# Rewrite the main domain
RewriteCond %{HTTP_HOST} !sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/mainsite/.*
RewriteRule ^/mainsite/(.*)$ /mainsite/$1 [L]
# Rewrite the sub domain
RewriteCond %{HTTP_HOST} sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/sub/.*
RewriteRule ^/sub/(.*)$ /sub/$1 [L]`
Related
so my subdomain currently goes to the same root folder as my domain. I would like to accomplish the following:
sub.domain.com => sub.domain.com/sub/
with the /sub/ portion invisible to the user.
I do not want to have the following happen:
sub.domain.com => domain.com/sub/
Is there any way I can accomplish this without redirection and without getting stuck in an endless loop? I have this currently and I don't think it's working as it doesn't mask the /sub part:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/sub
RewriteRule .* http://sub.domain.com/sub[L]
I've looked endlessly online for something that accomplishes this but can't seem to find it...
I figured it out through a bunch of trial and error:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/sub
RewriteRule ^(.*)$ sub/$1 [L]
I want to have a multi language site. Now, I have 2 domains. The first one is the main domain. That is website.nl. And i have a domain alias, website.org. So the 2 domains share the same public_html folder.
What I want is that:
website.nl will use the file /index.php/$1 and
website.org will use the file /gb/index.php/$1 (So when the url is website.org/test you will use the file /gb/index.php/test (No url redirect)
I found on another topic on stackoverflow the following:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} website.org
RewriteRule ^(.*)$ /gb/index.php [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
But this htaccess file won't work. I will get a 500 error. That's all.
Can someone see what's going wrong?
Your rules are looping, otherwise the 2 rules will mess with each other and loop indefinitely (e.g. requesting /foo will result in /index.php/index.php/index.php/index.php... etc thus returning 500). You need to add some conditions to stop the looping. Try changing the conditions and rules to:
RewriteCond %{HTTP_HOST} website.org
RewriteCond %{REQUEST_URI} !^/gb/index.php
RewriteRule ^(.*)$ /gb/index.php/$1 [L]
RewriteCond %{REQUEST_URI} !^/gb/index.php
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ /index.php/$1 [L]
A bit of help fellow SO people.
What I have at the moment (based on some code I used for a different type of URL).
I want the first URL to redirect to the second, with no query string included afterwards
This is what I have to so far.
RewriteRule ^(page.php?id=missionstatement+)/?$ http://example.com/why/mission-statement [R=301,L]
RewriteRule ^(page.php?id=ofsted+)/?$ http://example.com/how/ofsted-report [R=301,L]
RewriteRule ^(page.php?id=governingbody+)/?$ http://example.com/governors [R=301,L]
Here is the rule (will redirect 1 URL):
RewriteCond %{QUERY_STRING} ^id=whatever
RewriteRule ^page\.php$ http://%{HTTP_HOST}/how/somehow? [R=301,L]
This rule intended to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
I have used %{HTTP_HOST} -- this will redirect to the same domain as requested URL. If domain name has to be different, replace it by exact domain name.
The ? at the end of new URL will get rid of existing query string.
Ahoy!
Give this a whirl:
#check mod_rewrite is enabled
<IfModule mod_rewrite.c>
#enable mod rewrite
RewriteEngine On
#set working directory
RewriteBase /
#force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]
#bootstrap index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page.php\?id=(.*)$ http://www.willans.com/page.php/$1 [R=310,L]
#end mod rewrite check
</IfModule>
It's been a while since i've done any web dev, but that should be a push in the right direction at least ;)
Note: I had to add spaces because it thought I was posting links...
I have two sites coming into one server and one folder (foo.com and bar.com).
Foo.com needs to point at a page named foo.htm under the root of the site.
It also has the requirement of not changing the URL.
If the url is bar.com it needs to be left alone.
If the full url is http://www.foo.com/ it needs to be switched to the equivalent of
http://bar.com/foo.htm
Does that make sense?
I have the following which works for every page except the root page, which isn't redirecting to foo.htm.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^(.*)$ http://www.bar.com/$1
RewriteRule ^$ /foo.htm [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^$ http://www.bar.com/foo.htm [L]
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteCond %{REQUEST_URI} ^/.
RewriteRule ^(.*)$ http://www.bar.com/$1
I'm not sure if it will run exactly like this as I can't test it - but if it doesn't work, just modify the regex in RewriteCond %{REQUEST_URI} .... possibly remove the slash and question mark... just try it.
Another edition to my .htaccess rewriting saga. Everything is now working to a degree but still have some issues.
I have numerous rules to move old urls to new urls, passing variables etc - however there are still a couple of things i need to add, and for the life of me cannot figure out.
I have 3 urls that are directing/rewriting as below.
1 www.mydomain.com/news/dentistry_dental/index.php
2 www.mydomain.com/news/dentistry_dental/index.php?month=April&year=2011
3 www.mydomain.com/news/dentistry_dental/article_detail.php?article=1234&title=some-title
These are redirected & rewritten perfectly to the new urls respectively
1 www.mydomain.com/dental_news/
2 www.mydomain.com/dental_news/April-2011
3 www.mydomain.com/dental_news/1234-some-title
However... Here is the problem #1 The following urls are also redirecting as below
4 www.mydomain.com/news/it_technology/index.php?month=April&year=2011
5 www.mydomain.com/news/it_technology/article_detail.php?article=1234&title=some-title
Which are also directing to the same urls as the dental redirects
4 www.mydomain.com/dental_news/April-2011
5 www.mydomain.com/dental_news/1234-some-title
Which shouldnt be happening. The it_technology news articles have now been removed so i wish to redirect them either to my homepage with a 410 or something similar, whichever is the best option really.
My current .htaccess looks as follows.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{REQUEST_URI} !/generator/
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]
RewriteRule ^products/dental-digital-imaging/([^/]*)_([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]
RewriteRule ^products/package_deals.php$ http://www.mydomain.com/products/dental-computer-network-bundles [R=301]
RewriteRule ^products/computer_hardware.php$ http://www.mydomain.com/products/dental-computer-solutions [R=301]
RewriteRule ^products/individual_computers.php$ http://www.mydomain.com/products/dental-computer-systems [R=301]
RewriteRule ^products/digital_xray_imaging.php$ http://www.mydomain.com/products/dental-digital-imaging [R=301]
RewriteRule dental_news/$ /news/dentistry_dental/?rewrite [L]
# Rewrite dental news article to neat nice url
# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite
RewriteRule ^dental_news/([0-9]*)-([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&rewrite [L]
#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1-%2? [L,R=301]
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule (.*) /dental_news/%1-%2? [R=301]
# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite
RewriteRule ^dental_news/([a-zA-Z]*)-([0-9]*)/?$ news/dentistry_dental/index.php?month=$1&year=$2&rewrite [L]
# Rewrite URL stripping .php Extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
My only other requirement would be to add a trailing slash to the php files served ( that have allready add the .php extension removed with the last rule in my .htaccess.
I have tried numerous ways that i have found via google, but all of them cuase probelms with my other rules.
Hopefully someone can help me finish this off once and for all.
Regards
M
Sure is getting complicated :) Try changing these lines (I think there are 2):
RewriteCond %{REQUEST_URI} !^/dental_news/
to:
RewriteCond %{REQUEST_URI} ^/news/dentistry_dental/
That way its only going to redirect the old dental news URLs to there respective new ones.
Trivially, your problem is the catch-all rules:
#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1-%2? [L,R=301]
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule (.*) /dental_news/%1-%2? [R=301]
Restrict them to the path you want to rewrite, i.e. replace .* with something more specific, like:
#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule ^news/dentistry_dental/index.php$ /dental_news/%1-%2? [L,R=301]
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule ^news/dentistry_dental/index.php$ /dental_news/%1-%2? [R=301]
You can do the news/it_technology redirect as a standard redirect.
RewriteRule ^news/it_technology http://www.thepage.com/ [R=301,L]
It might technically be more correct to do a 410, but the 410 code is really optional. You might also want to make it 404, and then have a link from the 404 page to the main page.