I am having a minor trouble in .htaccess file, I have looked into some stack links, but couldn't resolve my issue.
I have hosted a multi domain site, what I want is: If there is a subdomain and I access this url:
subdomain1.example.com/sitemap.xml
subdomain2.example.com/sitemap.xml
This should pick up the sitemap.xml file from subdomains/subdomain1 and subdomains/subdomain2 respectively, which works great with this rule:
RewriteCond %{HTTP_HOST} ^(.*)\.example(.*).com [NC]
RewriteRule ^(sitemap.xml|robots.txt)$ subdomains/%1/$1 [L,NC]
If there is www.example.com then pickup the sitemap.xml from project root folder.
I have tried this, but not working:
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(sitemap.xml|robots.txt)$ http://www.example.com/$1 [L,NC]
It gives me 500 internal server error.
Your RegEx condition in RewriteCond %{HTTP_HOST} ^(.*)\.example(.*).com [NC] matches even www.example.com, so there will be a redirect loop.
The rewrite condition alternative that you specified as RewriteCond %{HTTP_HOST} ^www.example.com [NC] won't work as it is does not match www.example.com
Try this Regex condition
^(?!www).*\.example(.*).com
OR
RewriteCond %{HTTP_HOST} ^(?!www).*\.example(.*).com [NC]
Related
Here's what I need.
I need to redirect (301) all content of blog.example.com/sample/ to example.com/sample/ and I'd like to accomplish this using htaccess.
Here's what I tried, but it didn't work:
RewriteCond %{HTTP_HOST} ^blog\.example\.com/sample [NC]
RewriteRule ^(.*)$ https://www\.example\.com/sample/$1 [L,R=301]
Thanks for the help.
RewriteCond %{HTTP_HOST} ^blog\.example\.com/sample [NC]
Here having /sample is problematic because HTTP_HOST only matches host name part in a request so it will never match this condition.
You can use this rule instead:
RewriteCond %{HTTP_HOST} ^blog\.(example\.com)$ [NC]
RewriteRule ^sample/ https://www.%1%{REQUEST_URI} [L,R=301,NC,NE]
I want to redirect the the subdomain to subdirectory but not working. Here is my efforts.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^name\.site_url
RewriteRule ^(.*)$ http://site_url/name/$1 [L,R=301]
Try this:
RewriteCond %{HTTP_HOST} ^sub\.
RewriteRule ^(.*)$ http://your_domain/sub/$1 [R,L]
Instead sub in RewriteCond and RewriteRule you can place whatever you want
Btw, RewriteCond %{HTTP_HOST} ^sub\.my\.domain$ also works for me. So, check your site url. Or give more info (at least, what happened in apache error.log).
I'm trying to redirect foo.bar.com to bar.com/foo.php, but I have no idea where to start and everything else has failed me so far. I've tried many things here on StackOverflow, but none of them seem to be working at all (just various Apache errors).
I already have a wildcard subdomain set up from cPanel, but I'm not really sure where to go from here. Is .htaccess even the best option for this?
EDIT: I also want to rewrite the URL from bar.com/foo.php to foo.bar.com.
You can use this generic rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(.+)$ [NC]
RewriteRule ^/?$ http://%2/%1.php [L,R=302]
EDIT: To hide the rewritten URL (internal rewrite) use this:
RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(.+)$ [NC]
RewriteRule ^/?$ %1.php [L]
I want to write a RewriteRule and RewriteCond, so that whenever sub domain is called different file is going to processed, with same url.
I have tried by following code, but's not working for me
RewriteCond %{HTTP_HOST} !^mydomain\.test\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.test\.com$ [NC]
RewriteRule ^$ /app/test/index.php?param=$1 [NC,QSA,L]
I have configured all other steps properly. Just every time its calling index file from www, not from mentioned folder.
You're not capturing $1 in your RewriteRule, change your code to this:
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.test\.com$ [NC]
RewriteRule ^((?!app/test/index\.php).*)$ /app/test/index.php?param=$1 [QSA,L,NC]
I have an htaccess rewrite for wildcard sub domains which rewrites foo.domain.com to domain.com/index.php?id=foo. This is done using:
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z]+)\.)domain\.com [NC]
RewriteRule .? index.php?id=%3 [L]
This works OK, but all content on the site is referenced to the root e.g:
"/content/imgs/logo.jpg" or "/ajax/upload.php"
The wild-card sub domain changes the root and all content is referenced to:
"http://foo.domain.com/content/imgs/logo.jpg"
And the content cannot be found because it is not located on this subdomain.
I know you can use the html < base > tags to place a prefix on all href locations but this does not help in javascript for ajax requests.
Is there anything that can be done in htaccess to solve this problem?
Thank you.
I think I may have the solution to your problem Christopher!
Your rewrite condition was being applied to ALL included content such ass css/js/images etc. Which mean it was trying to rewrite this content to index.php?id=%3 instead of style.css.
By adding the line RewriteCond %{REQUEST_FILENAME} !-f above the condition and placing the entire condition at the bottom of the htaccess means that it will only apply to files/directories that DO NOT EXIST (i.e. only to wildcard subdomains).
Try the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z]+)\.)domain\.com [NC]
RewriteRule ^(.*)$ "http://domain.com/index.php?id=%3" [L,P]
Hope this helps you mate!
W.
Ok, one problem right away, you aren't specifying a target domain on your rewrite rule. If all requests from any subdomain except www need to be redirected, you should specify that like:
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z]+)\.)domain\.com [NC]
RewriteRule .? http://www.domain.com/index.php?id=%3 [L]
The other problem with this is that you are ignoring the file path after foo.domain.com, so that ALL requests get redirected to domain/index.php?id=foo regardless if the requested file was a page or an image.
A better way might be to give each "subdomain" its own folder, something like
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z]+)\.)domain\.com [NC]
RewriteRule ^(.*)? http://www.domain.com/%3/$1 [L]
so a path like foo.domain.com/content/imgs/logo.jpg would point to domain.com/foo/content/imgs/logo.jpg