Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

rewrite to www.site with modx dynamic seo urls



4:23 am on Feb 4, 2008 (gmt 0)

10+ Year Member

I've been trying to figure this one out, and I just can't see how to do it.

modx has some nice search engine friendly urls, the goal here is to get all urls to get a www prepended if absent.

So far, I'm doing this, the site is being developed, and this works, but fails to create the www except for / requests.

Also, this seems very hackish, I know there must be a better way to get even that done:

RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*)$ index.php?q=$1 [QSA]

I'm just using this for now until I get it actually running.

Requests for http: //example.com/ are correctly rewritten to http: //www.example.com/

Requests for internal pages are ignored, not being either -f or -d, and are handled by the QSA rule.

However, the goal here would be to get requests for example.com/some/virtual/url rewritten to www.example.com/some/virtual/url

I've tried a variety of permutations, but if I remove the -f and -d conditions, I get an internal modx 404 error page, which I can then follow the navigation out of and surf the site. Currently actual nav links are are done to a base url of www.example.com, so it only seems to the first landing page that causes the problem.

Hopefully this is clear.


2:31 pm on Feb 4, 2008 (gmt 0)

10+ Year Member

I redirect non-www to www on my site with the following in .htaccess

RewriteEngine on
RewriteBase /

[some other rewrites]

RewriteCond %{HTTP_HOST}!^www\.example\.co\.uk [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule ^(.*) http://www.example.co.uk/$1 [L,R=301]

Hope this is what you are looking for.


7:38 pm on Feb 4, 2008 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Here's the correct 'classic' .htaccess solution; If any errors occur with this, then you've got a problem outside of this code:

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?q=$1 [QSA,L]

It's possible to get a good speed-up of your server by excluding certain URL-spaces from being checked for 'exists' in the second rule. For example, it's not necessary to rewrite requests for index.php itself, and depending on your site's URLs, you might be able to exclude images, CSS, external JS scripts, or even any URL with a trailing slash or a period in the final path-part from being processed through the file- and directory-exists checks. Since these checks involve a call to the file system --and possibly to the disk-- it's best to avoid doing them unless you have to. On a busy site, avoiding unnecessary filesystem checks can make a visible difference in server response.


# Bypass rule for URLs with "." in final path-part
RewriteCond $1 !^([^/]+/)*[^./]*\.[^/]+$
# Bypass rule for URLs with trailing slash
RewriteCond $1 !/$
# If requested URL-path does not resolve to an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Internally rewrite request to index.php with URL-path as query string
RewriteRule (.*) index.php?q=$1 [QSA,L]



8:23 pm on Feb 4, 2008 (gmt 0)

10+ Year Member

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule (.*) index.php?q=$1 [QSA,L]

That did it. I almost thought it didn't, but then I realized I had slightly changed your code, removing the L in [R=301,L], which of course then cascades down to the QSA conditions, which then resulted in an internal modx 404.

As soon as I used your exact code, which I think I now understand, correct me if this is wrong, it worked as expected:

1. Initial request is made to apache for non www url.
2. Request is 301ed to www url, and that process ends.
3. New request for www url is received, and that request is then handled by the QSA condition.

I was almost going to say this didn't work, but then I remembered who was giving the advice, in which case, it's far more likely I did something slightly wrong.

The . case is interesting, I didn't know about that option, that's the clean code I suspected existed and which my hack was trying to emulate.

I'm going to take a closer look at the optimizing code, not for this site, which will never see enough visitors to warrant that much dev time, but for one of my more popular sites, that might benefit from such optimizations.

Thanks a lot for your input, that's one glitch I couldn't resolve on my own, and thanks for your ongoing Apache support.


Featured Threads

Hot Threads This Week

Hot Threads This Month