.htaccess rewriterule giving a 500 error - .htaccess

I'm using virtual subdomains to catch all traffic to my site and using the subdomain as an identifier in an app. All traffic to a non-subdomain URL should use index.html; but I'm not having any luck getting the redirect to send subdomain traffic to the correct page. So far I've got:
suPHP_ConfigPath /var/sites/w/domain.org/
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.org$ [NC]
RewriteCond %{HTTP_HOST} !^domain.org$ [NC]
RewriteRule ^(.*)$ /sub.html
The index page loads on a domain URL, but a subdomain URL gets a 500 error, instead of redirecting to http://sub.domain.org/sub.html (note, it's important that I keep the subdomain in the redirected URL).
Can anyone point me in the right direction?

Try including a condition to check if the URI is already /sub.html
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.org$ [NC]
RewriteCond %{HTTP_HOST} !^domain.org$ [NC]
RewriteCond $1 !^sub\.html
RewriteRule ^(.*)$ /sub.html

Related

htaccess HTTPS redirect excluding HTTPS subdomains

I know it has been asked many times here, but can't find the right configuration for my setup.
Requirements:
http://www.example.com & https://www.example.com (& example.com) have to redirect to https://test.example.com . (Notice it is a subdomain)
https://sub_1.example.com is to be EXCLUDED and not redirected. (Notice the subdomain has an 'underscore')
What I Have:
RewriteEngine On
RewriteBase /
# EXCLUDE following sub-domains.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^sub_1\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^test\.example\.com$ [NC]
# REDIRECT to
RewriteRule (.*) https://test.example.com/$1 [R=301,L]
>> ISSUE:
My https://sub_1.example.com is in fact an API that feeds a mobile app. When I use the above code, the mobile fails. So it seems that the sub-domain is not properly excluded and protected from the re-direct.
Would appreciate some help in cleaning it up.
FIXED !!
Needed to add an exception before the existing rules, since I needed to exclude https://sub_1.example.com/api
RewriteCond %{HTTP_HOST} ^sub_1\.example\.com [NC]
RewriteRule ^api - [L]

301 redirect from non-www to www version

I'm trying to redirect my non-www site to its www version, to this end I put the below code in the .htaccess in root folder of my site:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
But problem is that after all this configuration I get this error when trying to access it:
The page isn’t redirecting properly
Both www and non-www address of my site doesn't work.
Is anything wrong with that code?
This situation arises on a server that hosts multiple domains. Mostly such servers are shared hosts or reseller type of hosting arrangements. Use the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?example\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
I've found the solution here

301 redirect to new domain with same url structure

I have a Magento website that is moving to a new domain. Im looking to 301 redirect all pages from the old domain to the new domain keeping same url structure.
I've updated the .htaccess file on my old domain with:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^new-domain.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]
The issue: The above seems to redirect every instance of: old-domain.com/subdir/whatever only just to the main domain: new-domain.com.
I'd be looking to redirect old-domain.com/subdir/whatever to: new-domain.com/subdir/whatever.
Any idea on what could be wrong?
Adjust your rule to use REQUEST_URI variable which is not dependent on the directory of .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://new-domain.com%{REQUEST_URI} [R=301,L,NE]
Better to clear your browser cache before testing this rule.
PS: You need to match hostname condition to old-host not the new-host.
This should redirect and retain the path after the main domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]

Prevent access of sub-/addondomain via htaccess

I have 2 domains: main.com and addon.net
On my shared hosting account I create an addon-domain foraddon.net which automatically creates a folder in the main-domain's directory as well as a subdomain.
I want to change the accessability of the addon domain via the maindomain:
http://addon.main.com
http://main.com/addon.net/
Now both serve the index.html from addon.net
Both URLs should result in a "404 - not found" error.
What I have right now on main.com/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?main.com$ [NC]
RewriteCond %{REQUEST_URI} ^/addon.net/(.*)$
RewriteRule ^(.*)$ 404.html [L]
And in addon.net/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.main.com$ [OR]
RewriteRule ^(.*)$ http://www.main.com/ [R=301,L]
And now everything redirects to main.com:
http://main.com/addon.net/ redirects to http://www.main.com
http://addon.main.com redirects to http://www.main.com
http://addon.net redirects to http://www.main.com
My question: which rules should I add to which .htaccess-file in order to get the desired results:
addon.main.com redirecting to main.com/404.html
main.com/addon.net redirecting to main.com/404.html
addon.net serving addon.net/index.html
If you want to redirect access from anything except addon.net (and redirect the other requests to a 404 page), all you need to do is use these lines in the .htaccess file inside addon.net :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?addon\.net$ [NC]
RewriteRule - /404.html [L]

Use htaccess to redirect all traffic from root to specific subdomain page

I have a site that has been up for some time. I had a blog on a subdomain for some time. I have decided to do away with the main site and just support the blog subdomain.
I have a redirect setup for this, but it carries all the extra parameters through to the blog which results in a file not found page appearing. I just want the redirect to go to the index page without parameters.
What I currently have in my .htaccess file is this
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?(.*)$ "http\:\/\/blog\.foo\.org\/index\.php" [R=301,L]
When I get a request to
http://www.foo.org/foo/foo/?module=foo
it redirects to
http://blog.foo.org/foo/foo/index.php?module=foo
I want it to redirect to
http://blog.foo.org/index.php
You have to specify the query in the replacement to override the original:
RewriteCond %{HTTP_HOST} !^blog\.example\.org$ [NC]
RewriteRule ^ http://blog.example.org/index.php? [R=301,L]
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^(www\.)?foo\.org$ [NC]
RewriteRule ^ http://blog.foo.org/ [R=301,L]

Resources