July 27th in Linux/Unix, PHP Scripts by .

Using htaccess to redirect domain without http://

Using htaccess to redirect domain without http

A very simple problem made me to sweat a lot. I have a site where users post their website link and most of them are so dumb and just wont put http:// in their domain and they submit just like www.domain.com.

If the link goes live in my website it wont get redirected and merely 404 file not found error pops up and the URL looks like http://mysite.com/www.domain.com

i know i could check http with regular expressions in php but was just so lazy to do that and this one magic line in .htaccess file solved my problem

RewriteRule ^www\.(.*)$ http://www.$1 [r=301,L]

This will redirect any domain without http like
http://mysite.com/www.domain.com -> http://www.domain.com

I hope this helps if your site is having the same problem.

Similar Posts:

One Comment

  • Glenn Zupan
    July 6, 2011

Leave A Comment.





Please wrap all source codes with [code][/code] tags. Powered by