Forum Moderators: phranque
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...
Post specific questions back here.
Thanks,
Jim
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]
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]
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]
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
... create a url that looks like:[domain.com...]
But I'd like to rewrite that to
[math.domain.com...]
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
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]
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.
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.