saurabh
By:
saurabh

Rewrite Rule in .htaccess file for subdomain

June 18, 2014 42.9k views
Hi, I have configured my Blog on http://blog.example.com. For this I have an Apache Conf file which direct the user to /blog directory. I have two major issues: 1.) After installation, when I try to open admin panel from http://blog.example.com/admin/login, it throws 500 Internal Server Error. It seems the rewrite engine is not properly coded in the .htaccess file. Also when I try to open the admin link, it takes me to: http://www.example.com/admin/login in place of http://blog.example.com/admin/login. This is bound to throw an error. 2.) When I open the site, http://blog.example.com, it opens normally, but when I click on the default 1st blog, again the Server throw 500 Internal Error and takes me to http://www.example.com/posts/my-blog in place of http://blog.example.com/posts/my-blog. I think it has something to do with .htaccess file which sends rewrite information to server, which is incorrect. Can you please advice what should be the edit for:
RewriteRule ^(.*)$ index.php/$1 [L]
so that I can remove index.php from the URL as well as keep the link to http://blog.example.com/ while browsing the blog?
10 Answers
Let's start simple: in your apache config (/etc/httpd/conf/httpd.conf or /etc/apache2/apache2.conf), do you have this uncommented:
NameVirtualHost *:80
If not, uncomment that, and restart httpd Next, most blogging software won't allow multiple domains in one folder. You'll need a separate folder per domain/sub-domain. If you're not doing this, you will need to separarate that. Almost done. Let's go back to the apache config (Look at my 2nd line for the file again) Do you have something like this for your vhost:
AllowOverride All
or is it something like this:
AllowOverride None
If the latter, it won't work, and you'll need to change it (This doesn't apply to the cgi-bin or error folders, just your document root). Next to last: do you have mod_rewrite enabled? For Ubuntu, you would run this:
a2enmod rewrite; service apache2 reload
Finally: do you have the permission set for the web user (apache/www-data) to have read access to your .htaccess file? If you're unsure of any of the answers to these questions, just let me know. Jon
Hi Checklist: 1.) Apache2.conf (CHECK) 2.) AllowOverrideAll (CHECK) 3.) mod_rewrite enables (CHECK) Pls advice what should be the correct code in .htaccess file. In my .htaccess file I have a code
RewriteRule ^(.*)$ index.php/$1 [L]
that Rewrites URL and remove index.php. But that's the catch. Once it removes index.php, it also remove any subdomain identifier take takes it back to "www". Pls advice correct code for .htaccess for subdomain redirection....
 RewriteCond %{HTTP_HOST} !^www.example.com$
 RewriteCond %{HTTP_HOST} ^blog\.example.com$
 RewriteRule ^/(.*)         http://blog.example.com/index.php/$1 [L, QSA]
That should work, but not knowing your layout, I'm having to guess a bit.
Hi My .htaccess file which is placed in root of my blog installation which is working to an extent now:
#Options -indexes

<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteBase /

	# Allow any files or directories that exist to be displayed directly
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d

	# Rewrite all other URLs to index.php/URL
	RewriteRule ^(([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]*))?)$ index.php?uri=$1&rewrite=page [L,QSA]
</IfModule>

<IfModule !mod_rewrite.c>
	ErrorDocument 404 index.php
</IfModule>
Even though I'm now able to stay on http://blog.example.com/ but I get errors: 1.) While I'm inside my blog's admin panel and trying to edit configuration. Now when I try to configure any setting, server throws "URL not found on sever" error. 2.) When I try to open admin panel from: http://blog.example.com/admin it throws an error saying file not found. But interestingly, when i explicitly put a "/" in the end like: http://blog.example.som/admin/ it takes me to admin panel 3.) Point to be noted, that I have another .htaccess file sitting on the root of http://www.example.com. It means that I have two .htaccess files. One is sitting on the root of "example.com/" and other sitting on "example.com/blog/". I don't know to what extent .htaccess file of example.com would have effect on "blog.example.com" (read as example.com/blog/). It seems I'm nearing a solution, but not yet done! Now I'm stuck. Neither I'm able to create any new blog as admin nor I'm able make any changes into admin configuration. Any guesses?
The rule I wrote says that it should take ANY non-existant file/folder and redirect it to index.php/$1 if you don't add a "/" at the end, it treats it as a file rather than a folder, so it falls under that rule, because you may have a folder named "admin", but not a file.
Hi While struggling I figured out that I'm a nood! :p I just copied your code and forgot to correct the space in:
RewriteRule ^/(.*) http://blog.example.com/index.php/$1 [L, QSA]
I again tried the correct code after removing the space:
RewriteRule ^/(.*)         http://blog.example.com/index.php/$1 [L,QSA]
and it did worked. Although now it is working with most of the features, yet, the "/" problem exists. I'm using a 'stop-gap' arrangement by putting a "/" in front of http://blog.example.com/admin/ to ensure I reach my Admin Panel however that is something not 'pro'. Can you suggest a remedy so that even if I don't put "/" it should still understand the URL? Unfortunately, the blog code forum not answering at all :(. There exists another peculiar issue, all the links generated on the Admin Panel through blog code, when clicked, send me to http://www.example.com/ which throws an error. Ain't these link bound to follow rules set in .htaccess file. I know I might be asking too much but I like things "the way they should be". Thanks for support in advance!

Is it wrtite code from www to non www version.

RewriteEngine On

RewriteCond %{HTTP_HOST} ^http://www.brother.printersupportaustralia.net$ [NC]
RewriteRule ^(.*)$ http://brother.printersupportaustralia.net/$1 [R=301,L]


Also Check this code for redirection with .index.htm to non www url

Options +FollowSymLinks

RewriteEngine on

RewriteCond %{HTTP_HOST} !^http://www.brother.printersupportaustralia.net [NC]

RewriteRule ^(.*)$ http://brother.printersupportaustralia.net/$1 [L,R=301]



RewriteCond %{REQUEST_URI} ^(.*)//(.*)$

RewriteRule . http://brother.printersupportaustralia.net%1/%2 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.htm\ HTTP/

RewriteRule index\.htm$ http://brother.printersupportaustralia.net/%1 [R=301,L]\


Have another answer? Share your knowledge.