homepage Welcome to WebmasterWorld Guest from 54.166.14.218
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
rewrite to www.site with modx dynamic seo urls
lizardx

10+ Year Member



 
Msg#: 3565542 posted 4:23 am on Feb 4, 2008 (gmt 0)

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.

 

mark_roach

10+ Year Member



 
Msg#: 3565542 posted 2:31 pm on Feb 4, 2008 (gmt 0)

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.

jdMorgan

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



 
Msg#: 3565542 posted 7:38 pm on Feb 4, 2008 (gmt 0)

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.

Example:

# 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]

Jim

lizardx

10+ Year Member



 
Msg#: 3565542 posted 8:23 pm on Feb 4, 2008 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved