Forum Moderators: phranque

Message Too Old, No Replies

ReWrite needed to handle subdomains and different page requests

Am I asking too much?

         

jonny123

10:17 am on Dec 18, 2008 (gmt 0)

10+ Year Member



I want to set up dynamic subdomains and urls on a site and need someone to give me a hand....

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?

jdMorgan

3:23 pm on Dec 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes all of this is possible. The only cases which are problematic are the "page=<somefolder>" cases. These cases can be implemented, but they are "dangerous" because they are inconsistent with the others, and so will require separate rules and special care to avoid unexpected results. The basic problem -even beyond any consideration of these rules and your specific problem here- is that a folder is not a page, and should not be treated as such.

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

jonny123

8:51 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



Thanks for that

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!

jdMorgan

12:28 am on Dec 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, it's not tricky, as long as you have a very, very clear set of rules for mapping subdomains and virtual-directory-paths in the requested static URLs to the query string variables used for calling your script.

> 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

jonny123

11:58 pm on Dec 20, 2008 (gmt 0)

10+ Year Member



Does anyone want to attempt this for me? It would be much appreciated!

jonny123

2:39 am on Dec 21, 2008 (gmt 0)

10+ Year Member



Is there anything wrong with this?.... it works, but are there any security issues?
I plan to send 404's back (from index.php) when the requested page isn't one I want to allow...

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?

g1smd

5:08 pm on Dec 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ahead of your rewrites make sure you place redirects to strip index filenames from requests (and fix www at the same time) as well as the general catch-all non-www to www redirect.

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.

jdMorgan

10:57 pm on Dec 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can significantly shorten that second 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]

Replace the broken pipe "¦" characters with solid pipe characters before use; Posting on this forum modifies the pipe characters.

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