Forum Moderators: phranque
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]
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.
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]
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]
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.