Hi m8s., I have the domain., ser.mydomain.com/cgi-bin/viewprofile.php?id=a123456 i would like to rewrite it as: mydomain.com/a123456 Also., if anyone types ser.mydomain.com/cgi-bin/viewprofile.php?id=a123456 it should have a 301 redirection made to: mydomain.com/a123456 would be grateful for any help Thanks Sarathy.s
You'll need to put in the right paths (depending how your subdomains are setup) but it should be something along the lines of: RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ser.mydomain.com RewriteCond %{QUERY_STRING} id=([a-z0-9]+) [NC] RewriteRule cgi-bin/viewprofile.php http://www.mydomain.com/%1 [R=301,NC,L] RewriteRule ([a-z0-9]+) /home/path/to/ser.mydomain.com/cgi-bin/viewprofile.php?id=$1 [NC,QSA,L] Code (markup): The first few lines do the 301 redirect if someone uses the long subdomain version. 1: Match host against the subdomain - we only want to do the redirect if they are on ser.mydomain.com 2: Look in the query string to capture the id= part. Using the range a-z and 0-9 to ensure we only get an alphanumeric id. 3: Send any requests for the viewprofile.php over to the shortened mydomain/X Then we do the rewrite that does it the other way round but without users knowing. You may or may not need to use absolute paths here - if its setup as in the example you will, but if your subdomain is just mydomain.com/ser then can use the relative path. Those rules will probably need a bit more tweaking but hopefully that helps.
Thanks rodney, The redirection is happening , But its like this: http://www.mydomain.com/C349?id=C349
Ah, try replacing this rule: RewriteRule cgi-bin/viewprofile.php http://www.mydomain.com/%1? [R=301,NC,L] Code (markup): The only difference is we're added an ? on the end so that we overwrite the query string (id=a123 with nothing) whereas by default it will carry it over to the rewrite destination.
Hi Rodney, Thanks now the redirection works, But the content of the subdomain profile is not being shown under mydomain.com/C349 Thanks again Sarathy.s
Have you put in the right path? RewriteRule ([a-z0-9]+) /home/path/to/ser.mydomain.com/cgi-bin/viewprofile.php?id=$1 [NC,QSA,L] The bit in bold needs to be edited to reflect the real location of your subdomain directory.
If you're sure the cgi-bin/viewprofile.php page exists, the next step would be enabling rewrite logging to see what's happening because I can't see the problem. If you add RewriteLog /home/path/to/somewhere/log.txt RewriteLogLevel 4 Code (markup): to the end of your httpd.conf and restart apache, it should show us where it's going wrong. Go to the domain.com/A123 page in your browser (just once or you'll have loads of data) then post the contents of the log.
Rodney, Just a doubt., There are 2 .htacess , one for domain.com and one for ser.domain.com I put all your codes in the .htaccess of the subdomain., Did i do it right? I will put the log codes now Thanks
No, put the subdomain part in the subdomain .htaccess and the other in the domain .htaccess. Subdomain: RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ser.mydomain.com RewriteCond %{QUERY_STRING} id=([a-z0-9]+) [NC] RewriteRule cgi-bin/viewprofile.php http://www.mydomain.com/%1? [R=301,NC,L] Code (markup): Domain: RewriteEngine On RewriteBase / RewriteRule ([a-z0-9]+) /home/path/to/ser.mydomain.com/cgi-bin/viewprofile.php?id=$1 [NC,QSA,L] Code (markup):
Hi Rodney, The redirection works properly., The problem is., the subdomain directory is not within the main domain, Its a seperate server., so i cant give a root path from the main domain's .htaccess Is there an alternate way>?
You cannot do url rewriting across different servers afaik, so you'll have to use some sort of script to work around it. Obviously the best option would be to actually move the subdomain files onto the main domain and there won't be any problems with using pretty URLs. Otherwise if that's not an option, I would suggest rewriting the requested page to a script on the main domain. The script would then get the correct page from the other server and display it - if you're doing it in PHP it could be as simple as a single include statement. The downside to this though is you'll be doubling your bandwidth usage - you send the page first from the subdomain server to the main server, then from there to the user.
thanks., That might affect the load balancer., (dont know anyway, i will forward this to my programmers) Thanks a lot