Forum Moderators: phranque
I really need the following... (is it possible?)
-------
[Rewrites for the domain - with/without the www]
example.com -> /index.php
www.example.com -> /index.php
[Rewrites for the domain - with/without the www - with a page request]
example.com/somepage.php -> /index.php?page=<somepage.php>
www.example.com/somepage.php -> /index.php?page=<somepage.php>
[Rewrites for the domain - with/without the www - with a page request - including one or more folders]
example.com/somefolder/somepage.php -> /index.php?page=<somefolder/somepage.php>
www.example.com/somefolder/somepage.php -> /index.php?page=<somefolder/somepage.php>
(and if a page is left blank - ie. index is requested)
example.com/somefolder/ -> /index.php?page=<somefolder>
www.example.com/somefolder/ -> /index.php?page=<somefolder>
-------
also...
-------
[Rewrites for the subdomain]
subdomain.example.com -> /index.php?subdomain=<subdomain>
[Rewrites for the subdomain - with a page request]
subdomain.example.com/somepage.php -> /index.php?subdomain=<subdomain>&page=<somepage.php>
[Rewrites for the subdomain - with a page request - including one or more folders]
subdomain.example.com/somefolder/somepage.php -> /index.php?subdomain=<subdomain>&page=<somefolder/somepage.php>
(and if a page is left blank - ie. index is requested)
subdomain.example.com/somefolder/ -> /index.php?subdomain=<subdomain>&page=<somefolder>
-------
And I really don't want the Google duplicate content problem - if the same page is accessible more than one way. Am I asking too much yet?
There are many previous threads here on subdomain-to-query-string rewriting and requested-page-to-query-string rewriting to help you get started.
One ting you should decide before starting is whether your script can accept a blank query string variable for <subdomain> or <somepage>. If so, this simplifies the rules by eliminating special cases. If not, then you just need more rules.
The duplicate content issue should be ignored until after you get your basic rewrites working; It is important to write and test only a few rules at a time to avoid problems. Once your internal rewrites are working, eliminating the duplicate content problem it is just a matter of 301-redirecting direct client requests for the dynamic URLs back to the static URLs.
Jim
Would it be any easier to send folders to another variable 'folder' to the querystring?
ie.
subdomain.example.com/somefolder/somefolder/somepage.php -> /index.html?subdomain=<subdomain>&folder=<somefolder/somefolder>&page=<somepage.php>
Im not sure what you mean your the third paragraph... but if it simplifies the rules then perhaps thats the way to go... can you please explain this for me.
I would like it to be able to handle blank/default page requests (index page) in the subdomain and folders (and combination of subdomain with folder(s))... is this a bit tricky?
Thanks again!
> I'm not sure what you mean [by your] third paragraph...
Simple. Access your script right now with the URL "example.com/index.php?subdomain=a-valid-subdomain&page=" -- That is, leave the "page=" value blank. If the script throws an error, then you need a special rule to call that script when the the rewrite rule cannot provide the page= value from the static URL. If the script doesn't throw an error when the "page=" value is blank, then you don't need to add a special rule to handle that case.
Jim
RewriteEngine On
#
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.example\.com [NC]
RewriteRule (.*) index.php?subdomain=%1&path=$1 [L,QSA]
#
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{SCRIPT_FILENAME} !^(.+).css$ [NC]
RewriteCond %{SCRIPT_FILENAME} !^(.+).js$ [NC]
RewriteCond %{SCRIPT_FILENAME} !^(.+).jpg$ [NC]
RewriteCond %{SCRIPT_FILENAME} !^(.+).gif$ [NC]
RewriteCond %{SCRIPT_FILENAME} !^(.+).png$ [NC]
RewriteRule (.*) index.php?path=$1 [L,QSA]
#
#
Also, something is set up on my webserver that is allowing a request of /style to return the file /style.css (ie. without its .css file extension). What should I tell my hosting company to turn off?
You'll also want (ahead of all of those) some redirects that capture direct dynamic-URL requests and 301 redirect to the correct static format, strip parameters, and fix www all in the same rule.
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond $1 !\.(css¦js¦jpg¦gif¦png)$ [NC]
RewriteCond $1 !index\.php$
RewriteRule (.*) index.php?path=$1 [L,QSA]
I also strongly suggest that you get rid of [NC] flags in both rules where possible. Accepting case errors creates massive duplicate-content problems, where the same content is accessible at many different URLs. Rather than accepting those mis-cased URLs in your internal rewrite rules, you should detect them and redirect them to the correct-cased URLs, or simply let them go 404-Not Found.
Jim