RegExp and Apache RewriteEngine (missing www.)

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

RegExp and Apache RewriteEngine (missing www.)

Post by visionmaster »

Hello together,

I would like to use Apaches mod_rewrite to redirect from http://webpage.com to http://www.webpage.com

The URL path may ofcourse hold a path of folders and some further parameters like so: http://webpage.com/folder1/folder2/mysr ... tion=start

RewriteCond %{HTTP_HOST} ^\.webpage\.com seems to be a good start. But how do I actually form the RewriteRule? I need to get hold of REQUEST_URI and QUERY_STRING (which can be empty or not). Howto?




Thanks,
visionmaster
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

What about something like:

Code: Select all

RewriteRule /^webpage.com(.*)$/ www.webpage.com$1
This is untested of course ubt does the idea make sense?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

I don't think regex let's you get at the absolute URLs in that manner, why not set up two Virtual hosts, one for www. and one without, have the one without just have an index.php that outputs a permanently moved header to redirect to the www. version
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it does allow you to do it, indirectly..

Code: Select all

RewriteEngine on
RewriteCond %{HTTP_HOST} ^webpage.com$
RewriteRule /(.*) http://www.webpage.com/$1
not tested, but from memory.. may be a tiny bit off.
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Post by visionmaster »

feyd wrote:it does allow you to do it, indirectly..

Code: Select all

RewriteEngine on
RewriteCond %{HTTP_HOST} ^webpage.com$
RewriteRule /(.*) http://www.webpage.com/$1
not tested, but from memory.. may be a tiny bit off.
@feyd thanks for your help, I was thinking much to complicated. It works like this:

Code: Select all

RewriteEngine on
RewriteCond %{HTTP_HOST} ^webpage.de$
RewriteRule (.*) http://www.webpage.de/$1

Thanks to you all!
Post Reply