Forum Moderators: phranque

Message Too Old, No Replies

rewrite a url for subdomains

subdomains

         

pdxbenjamin

4:49 pm on Feb 7, 2009 (gmt 0)

10+ Year Member




I wish to write a small app for some friends...
I'm having trouble with the rewrite.

The app will have a submit form to enter name, subject and class. To create a url that looks like:

[domain.com...]

But I'd like to rewrite that to

[math.domain.com...]

Thanks in advance...

jdMorgan

5:48 pm on Feb 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please see Changing Dynamic URLs to Static URLs [webmasterworld.com] in our Apache Forum Library to get started.

Post specific questions back here.

Thanks,
Jim

pdxbenjamin

6:34 pm on Feb 7, 2009 (gmt 0)

10+ Year Member



Hi thank you for the quick reply..

I have this example of the dynamic to static url working. I've been able to create pretty urls before witch this is an example of...

but how do I get the first varable, s=math to move to the subdomain ?

To have an end result of this [math.domain.com...]

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^/([^/]+)/([^/]+)/?$ /index.php?product=$1&color=$2 [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?product=([^&]+)&color=([^&]+)\ HTTP/
RewriteRule ^index\.php$ [domain.com...] [R=301,L]

g1smd

6:41 pm on Feb 7, 2009 (gmt 0)

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



When reading those topics be clear you have a URL (used out on the web) like http://math.domain.com/math101 and a server filepath (used inside the server) like /index.php?s=math&c=math101

Remember that a rewrite translates a URL request into the required server path used to fetch the content.

To be clear, you need to do any external fix-up of the requested URL by using a redirect. Any such redirect must be listed before any of the rewrites.

[edited by: g1smd at 6:43 pm (utc) on Feb. 7, 2009]

jdMorgan

6:42 pm on Feb 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does this code go into a server config file such as httpd.conf, or into an .htaccess file? Either way, one of your rules won't work, depending of which file this code is in...

Jim

pdxbenjamin

6:48 pm on Feb 7, 2009 (gmt 0)

10+ Year Member



Jim,

this goes into my .htaccess...


Benjamin

jdMorgan

8:04 pm on Feb 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



These modifications make your external redirect and internal rewrite "mirror images" of each other. The additional second and third redirects force canonicalization of the "virtual URLs" and the domain, respectiviely. This is necessary to avoid duplicate content (the same content appearing at more than one unique URL) and to be sure that later rules can be simplified but won't fail because of an added slash or a non-canonical hostname.

Options +FollowSymLinks
RewriteEngine on
#
# Redirect direct client requests for dynamic URLs to corresponding static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?s=([^&]+)&c=([^&])&product=([^&]+)&color=([^\ ]+)\ HTTP/
RewriteRule ^index\.php$ http://%1.example.com/%2/%3/%4? [R=301,L]
#
# Redirect non-canonical domain and www subdomain requests to canonical domain
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.com(\.¦\.?:[0-9]+)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]+)+\.([^.]+)\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} [A-Z]
RewriteRule (.*) http://example.com/$1 [R=301,L]
#
# Redirect to remove trailing slash unless URL resolves to an existing physical directory
RewriteCond %{REQUEST_FILEPATH} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
#
# Internally rewrite requests for static URLs to dynamic filepath and query string
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ /index.php?s=%1&c=$1&product=$2&color=$3 [L]

Replace the broken pipe "¦" character above with a solid pipe character before use; Posting on this forum modifies the pipe characters. (2nd rewritecond pattern in 2nd rule)

You will need to modify these rules if your parameters are not in the given order, or add additional rules if some of the parameters may not always be present. I strongly suggest that you enforce parameter order and presence in all URLs and script filepath query strings. If you don't, the number and complexity of your rules can quickly spiral out of control.

In fact, they are complicated enough as they are with the mix of subdomain- and virtual-directory- carried variables; I'm not actually sure this code is comprehensively correct for what you want.

Note that the rules are in order: External redirects first, ordered from most-specific URL patterns and conditions to least-specific, followed by internal rewrites, again from most-specific to least specific. There are exceptions (for example, for mutually-exclusive-pattern rules), but you will have the least trouble if you stick to this general rule of thumb.

Jim

jdMorgan

8:16 pm on Feb 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One more comment:
... create a url that looks like:

[domain.com...]

But I'd like to rewrite that to

[math.domain.com...]


It is very important to grasp that whatever URL appears on your HTML page *is* the URL. URLs are defined by Web pages, and you cannot "rewrite" a URL. The URl on you page is what search engines wiil use in their index, and any attempt to use a redirect to "fix your URLs" while still publishing "incorrect" URLs on your own pages will confuse the search engine spiders and very likely lead to non-optimal indexing.

Ignoring the SEO aspect, you can externally redirect a requested URL to another URL. Or you can internally rewrite a requested URL to a different internal server filepath than the one that it would resolve to by default (if you didn't use a rewrite).

The URLs appearing in links and POSTs on your pages must be changed to the new static/SEO friendly format. This is the first step as described in the thread I cited at the outset. Only when that has been done can the internal rewrite (step 2) and the client-dynamic-URL-request-to-static-URL redirect (optional step 3) all work properly.

Jim

pdxbenjamin

11:29 pm on Feb 7, 2009 (gmt 0)

10+ Year Member



I thank you for all your help, however I've been playing with this .htaccess script all afternoon and still can not get it to work.

I set up my form that has two fields, a class and subject. This all works fine. I use two files, an index.php submitting to test.php...

My url from the address bar looks like this.
[domain.com...]

I can not achieve the desired effect where the variable S=mouse will become the sudo/virtual subdomain... and become this [mouse.domain.com...]

This is what i'm currently working with...

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php\?s=([^&]+)&c=([^&])\ HTTP/
RewriteRule ^test\.php$ [%1.rblclothing.com...] [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.rblclothing\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^rblclothing\.com(\.¦\.?:[0-9]+)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]+)+\.([^.]+)\.rblclothing\.com [NC,OR]
RewriteCond %{HTTP_HOST} [A-Z]
RewriteRule (.*) [rblclothing.com...] [R=301,L]

RewriteCond %{REQUEST_FILEPATH} !-d
RewriteRule ^(.+)/$ [%{HTTP_HOST}...] [R=301,L]

RewriteCond %{HTTP_HOST} ^([^\.]+)\.rblclothing\.com
RewriteRule ^([^/]+)/([^/]+)/$ /test.php?s=%1&c=$1 [L]

g1smd

11:40 pm on Feb 7, 2009 (gmt 0)

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



This rewrite

RewriteRule ^([^/]+)[b]/[/b]([^/]+)[b]/[/b]$ /test.php?s=%1&c=$1 [L]

needs

/word1[b]/[/b]word2[b]/[/b]
after the domain name, but you are only feeding it
/keyboard/
here.

g1smd

11:48 pm on Feb 7, 2009 (gmt 0)

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



Your previous redirects redirect to a URL that does NOT end in a trailing slash, but your rewrite only accepts URL requests WITH a trailing slash.

Using a URL without a trailing slash is correct for "extensionless URLs".

.

Do also make sure that your rule, once you have the extensionless part working does properly reject image requests, robots.txt requests and so on, because those URLs should still be requested with extensions. The danger is that .* and .+ match "everything" - if the rules are not specific enough to reject a URL with a dot on it, they could inadvertently "grab" those requests and send them towards your script for processing.

pdxbenjamin

11:58 pm on Feb 7, 2009 (gmt 0)

10+ Year Member




Change the line to this...
RewriteRule ^([^/]+)/([^/]+)/$ /test.php?s=%1&c=$1&product=$2&color=$3 [L]

or this?

RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ /test.php?s=%1&c=$2 [L]

Have you tested the rewrite, because i must be doing something wrong?

g1smd

12:13 am on Feb 8, 2009 (gmt 0)

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



You need to state both the (test URL) and the (script internal name with filepath and parameters) in order to be able to work that out. I certainly could not guess.

I was just looking at your example URL in #3844743 along with the rules you posted.

By the way, where you say "I can not achieve the desired effect where the variable S=mouse will become the sudo/virtual subdomain" do be aware that htaccess does not "make" URLs.

URLs are made in links on a page. When a user clicks such a link, htaccess takes that request and gets the content from a different location to what the URL suggested it might be, but without letting the user know what that location actually was.

pdxbenjamin

1:39 am on Feb 8, 2009 (gmt 0)

10+ Year Member




ok i understand, thank you everyone for your input and suggestions and time on this topic.