Forum Moderators: phranque

Message Too Old, No Replies

Subdomain / mod rewrite assistance

         

Awagner

8:35 pm on Jun 29, 2007 (gmt 0)

10+ Year Member



Hello,
I've set up my domain as such that when a user types

parachute.mydomain.com

they are redirected to

mydomain.com/#*$!/index.php?au=parachute

is there anyway to tidy up the URL they are directed to so that

mydomain.com/#*$!/index.php?au=parachute

is rendered simply as parachute.mydomain.com?

does that make sense?

Any help is greatly appreciated

jdMorgan

12:42 am on Jun 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use an internal rewrite instead of an external redirect.

This is one of the two main functions of mod_rewrite, and which it does depends on the syntax you use.

If you externally redirect, then the server response tells the client re-request what it just asked for, using the new URL provided in the server redirect response.

If you internally rewrite, then you simply tell the server, "If this URL is requested, serve the contents from that file" -- The client is not involved. And you can just as easily substitute "If this URL is requested, generate the contents using the script in that file" for PHP-driven sites.

A redirect is a URL-to-URL translation, while a rewrite is a URL-to-non-default-filepath translation.

Jim

Awagner

9:57 pm on Jun 30, 2007 (gmt 0)

10+ Year Member



The subdomains are dynamic though. I can show you what I have for the .htaccess

Needless to say i'm relatively new with mod_rewrite

Can you show me an exaple .htaccess setup that would provide for this situation?

jdMorgan

7:02 pm on Jul 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> The subdomains are dynamic though. I can show you what I have for the .htaccess

That makes no difference. The only change is to the syntax of your code; Change the rule so that it does an internal rewrite to the subdomain's file directory instead of doing an external redirect which exposes the subdirectory path to the visitor.

You may need to add a RewriteCond to prevent internal rewrite looping, but is all that is needed.

Jim

kael

6:40 pm on Jul 3, 2007 (gmt 0)

10+ Year Member



I've come across a similar problem, I'm hoping you can help as we're stuck. Yes, we're using Godaddy and they're not much help. I've heard this being done before 1) rewrite on a subdomain and 2) rewrite on a dynamic URL

We have the mod-rewrite working 1/2 way. The rewrite is working to take the user to the right page, but when we attempt this via a subdomain it does not work.

We have the .htaccess file in both the root of the site and in the subdomain folder.

So www.example.com/page1/foo.html is working in the rewrite, but http://subdomain.example.com/page1/foo.html is not working.

.htaccess file info below.

================
Options +FollowSymLinks
RewriteEngine on
RewriteBase /whistler
RewriteRule ^http://whistler.example.com/(.*)/(.*).htm$ http://www.example.com/demo/lease_show.php?Lease=$2%{QUERY_STRING}
rewriteRule ^foo\.html$ http://www.example.com/index.php

===========================

Also, the other rewrite we're trying to do is to redirect a dynamic URl to a static URL. For some reason this is not working. The URl http://whistler.example.com/pemeberton/32.htm url should access the file www.example.com/demo/lease_show.php?Lease=32

Any thoughts why this is not working?

======================
Options +FollowSymLinks
RewriteEngine on
RewriteBase /demo
RewriteRule http://whistler.example.com/(.*)/(.*).htm$ http://www.example.com/demo/lease_show.php?Lease=$2%{QUERY_STRING}
==========================

[edited by: jdMorgan at 8:32 pm (utc) on July 3, 2007]
[edit reason] No URLs, please. See Terms of Service. [/edit]

jdMorgan

8:51 pm on Jul 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In both cases, the code is badly-formed, in that RewriteRule 'sees' only the local URL-path, not the protocol, domain, URL-path, and query string. All path info that leads to the directory in which the .htaccess file containing the mod_rewrite code resides is stripped, and query strings must also be handled separately.

For example, if the URL http://www.example.com/dir1/dir2/dir3/resize-logo.php?size=large is requested, then a RewriteRule residing at http://www.example.com/dir1/dir2/.htaccess would 'see' only a local URL-path of dir3/resize-logo.php -- again, all of the path info to the .htaccess file's directory is removed.

Also, it seems you've missed the distinction between an external redirect and an internal rewrite.

Based on what you've written, I'd suggest:


Options +FollowSymLinks
RewriteEngine on
RewriteBase /whistler
RewriteCond %(HTTP_HOST) ^(www\.)?whistler\.example\.com
RewriteRule ^(([^/]+)/)+([^.]+)\.htm$ /demo/lease_show.php?Lease=$3 [QSA,L]
#
# Simple test rule
RewriteRule ^foo\.html$ http://www.example.com/index.php [R=301,L]

---

Options +FollowSymLinks
RewriteEngine on
RewriteBase /demo
RewriteCond %{HTTP_HOST} ^(www\.)?whistler\.example\.com
RewriteRule ^(([^/]+)/)+([^.]+)\.htm$ /demo/lease_show.php?Lease=$3 [QSA,L]

You may not even need the hostname-checking RewriteConds. I left them in so that the code is equally-specific to your original. The RewriteRule patterns have been optimized for efficiency -- Avoid using multiple (.*) patterns in a single rule whenever possible. The [QSA] flag is used to append the new query string to any existing query string, so you don't have to do it manually.

Jim