Forum Moderators: phranque

Message Too Old, No Replies

rewrite url and link problem

problem with link problem and rewrite url

         

robert gsfame

2:46 am on Aug 28, 2010 (gmt 0)

10+ Year Member



I am new to .htaccess and i really need helps..

Several questions:
* i try to create a virtual subdomain by rewriting url using .htaccess

<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www\.mydomain\.com\.th$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+)\.mydomain\.com\.th [NC]
RewriteRule (.*) mypage.php?username=%1 [L]
RewriteCond %{HTTP_HOST} ([^.]+)\.mydomain\.com\.th
</IfModule>
ErrorDocument 404 /page_error.html

everything goes normal, i can open mypage.php?username=robert by typing either www.robert.mydomain.com.th and without www.
but then i have problem when clicking on the link <a href> as i cant go anywhere although i saw that the url has changed


* second question
as i have made user virtual subdomain using above code, how can i rewrite this url

myfirstpage.php?username=robert&product=2

but i wish to hide the username, so it goes like

robert.mydomain.com.th/myfirstpage/product/2



Many thx in advance

jdMorgan

3:05 pm on Aug 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let's do one question at a time here, because your code appears to be broken.

One problem is that you have a RewriteCond *after* the RewriteRule, which won't do anything to that RewriteRule. That may be a problem since I don't know what your intent was. I removed it because I do not think you need it... But I could be wrong.

That mis-placed RewriteCond would also affect any RewriteRule that you added later below it.

More importantly, you have nothing in this rule to prevent looping.

The www.robert.mydomain.th to robert.mydomain.th redirect must be being done elsewhere, because your code as shown here would not allow access using www.robert.mydomain.th. Since you say that works, there must be some other code elsewhere that is handling this.

The <IfModule> container is only needed if you want this code to fail silently if mod_rewrite is not installed. That would make finding the problem more difficult...

I'd suggest:

ErrorDocument 404 /page_error.html
#
Options +FollowSymLinks Options +Indexes -MultiViews
RewriteEngine on
RewriteBase /
#
# Externally redirect to canonicalize all hostnames (remove extra "www", trailing FQDN
# period, port numbers, and redirect mydomain.com.th to www.mydomain.com.th
RewriteCond %{HTTP_HOST} ^www.([^.]+\.)mydomain\.com\.th [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]+\.)www\.mydomain\.com\.th [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]+\.)mydomain\.com\.th(\.?:[0-9]+|\.)$ [NC,OR]
RewriteCond www.%{HTTP_HOST} ^(www\.)mydomain\.com\.th [NC]
RewriteRule ^(.*)$ http://%1mydomain.com.th/$1 [R=301,L]
#
# Internally rewrite <user>.mydomain.com.th/<anything> requests to myfirstpage.php?username=<user>
RewriteConde $1 !^mypage\.php$
RewriteCond %{HTTP_HOST} !www\.mydomain\.com\.th$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9][a-z0-9\-]*[a-z0-9])\.mydomain\.com\.th [NC]
RewriteRule ^(.*)$ /mypage.php?username=%1 [L]

With this code, your "mypage.php" script must examine the server variables to get the requested page or object URL-path, because your rewrite rule does not forward the requested resource URL-path to your script.

For example, how do you plan to handle a request for "robert.mydomain.com.th/roberts-logo.gif" or "robert.mydomain.com.th/styles.css"? What about "robert.mydomain.com.th/robots.txt"?

An alternative would be to pass the "requested object" to your script as well, for example, by changing the rule to:

RewriteRule ^(.*)$ /mypage.php?username=%1&rqstobj=$1 [L]

and modifying your script to accept the rqstobj query parameter.

Jim

robert gsfame

5:04 am on Aug 29, 2010 (gmt 0)

10+ Year Member



You are right, i create virtual subdomain redirection by adding (*) inside cpanel ---> subdomain

as i dont have any idea at all about .htaccess and still learning ,so script just copied from several sources and being added directly.

anyway i have tried your code especially for the redirection from non www to www run perfectly.. but i still found that the php page appear,

www.robert.mydomain.com.th/mypage.php?username=robert
robert.mydomain.com.th/mypage.php?username=robert

i only wish to have this www.robert.mydomain.com.th or robert.mydomain.com.th without being followed with the php page

really need your helps...and wish that you can point me out any great tutorial links or may be books for .htaccess

Many thanks
Robert

jdMorgan

12:09 pm on Aug 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, as I said, you will need the find the reason that the filepath is exposed to the client as a URL. The code I posted will not do that, so the reason must be in some other code -- either in the server config files, in other .htaccess files, or in one of your scripts. An external redirect is being invoked in one or more of those locations, and you must fix that, because it is causing this problem.

Jim

robert gsfame

2:13 pm on Aug 30, 2010 (gmt 0)

10+ Year Member



Awesome! you re really good! and this forum is really recommended...Thx a lot! i will take time to learn more about .htaccess

many thanks
Robert