Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I've been at this all day and I thought I had a handle on regular expressions but I'm no longer confident that I do. I've searched all over, read through some horrible guides on mod_rewrite and I'm stumped.

I want to make it so that the call

subdomain.domain.com/u/satbirkira 

looks exactly like the above, but executes and displays this instead

 domain.com/user.php?user_name=satbirkira?subdomain_given=subdomain

It should be extremely simple but I can't get it. Here's what I came up with:

RewriteCond %{HTTP_HOST} ^(.*)user.php\?user_name=(.*)$ [NC]

RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/u/$3

I have no idea why some RewriteCond break if you remove useless capture points like the first (.*) but I've learned to just roll with it. I would appreciate it if someone would explain why the approach is incorrect, syntax, capture points and the replacement as well as variables.

Edit

Its similar to the goal on #4 on http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html, "4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz" expect the answer makes no sense to me.

share|improve this question
    
Would you like the user to enter the /user.php?user_name=satbirkira URL and get the content from /u/satbirkira? Or the other way around? –  Shane Madden Dec 11 '13 at 4:58
    
No, the other way around. Apologies. Given /u/satbirkira, it would compute and display /user.php?user_name=satbirkira but in actuality the user would still only see /u/satbirkira in the url. –  Satbir Kira Dec 11 '13 at 5:05
    
As it is now your RewriteCond will never match. Thus your RewriteRule never gets executed. –  Krist van Besien Dec 11 '13 at 12:36
    
As @KristvanBesien says, you've got the expressions turned around. The RewriteCond should match what is in the browser, i.e. subdomain.domain.com/u/satbirkira. –  Jenny D Dec 11 '13 at 13:23

2 Answers 2

up vote 1 down vote accepted

The ?user_name=satbirkira part can be a bit tricky to feed into PHP with a rewrite, depending on how PHP is accessing the data - some applications end up getting the original query string, and not the rewritten one.

But, for now, let's start with this and see where it gets us. This should be in VirtualHost context, if it's in <Directory> or .htaccess it'll need to be tweaked..

RewriteRule ^/u/([^/]+)/?$ /user.php?user_name=$1 [L]

For .htaccess (which you shouldn't use!):

RewriteRule ^u/([^/]+)/?$ user.php?user_name=$1 [L]
share|improve this answer
    
I would in the best case use a .htaccess placed in the main directory. I realized the way I wanted to plan the site to work conflicts with what the implementation actually should do. Given subdomain.domain.com/u/satbirkira, it should actually execute and display domain.com/user.php?user_name=satbirkira as the subdomain should not be there due to the server thinking its an actually sub-directory when it really isn't. From my understanding, this is would be RewriteRule ^(.*.com)/u/([^/]+)/?$ $1/user.php?user_name=$2 [L] for a virtualhost context, is there a way to get the .htaccess form? –  Satbir Kira Dec 11 '13 at 6:27
    
@SatbirKira Don't use .htaccess unless you need to modify it at runtime. See here. As far as the subdomain - how are your virtual hosts set up for the main domain versus the subdomain? Trying to point to one virtual host from another via mod_rewrite will generate a redirect, which is not what you want - you need to be able to internally redirect to the php file. –  Shane Madden Dec 11 '13 at 6:38
    
I don't actually have or want subdomains. I added a A type record for *.domain.com to point to the servers IP so that when I do get the url request subdomain.domain.com/u/satbirkira I can simply just execute and display domain.com/user.php?user_name=satbirkira?subdomain_given=subdomain. I can't edit the httpd config since its not a dedicated server. –  Satbir Kira Dec 11 '13 at 7:18
    
@SatbirKira Then I think the biggest issue will be getting the subdomains handled in the right way. How will you get the server configured to have the subdomains directed to your content - does the host have something that allows for this? –  Shane Madden Dec 11 '13 at 16:33
    
Turns out all I needed to do was have a wildcard for subdomains, not just the A type record. Everything works fine now. Thanks. –  Satbir Kira Dec 11 '13 at 18:21

This ought to work:

RewriteCond %{HTTP_HOST}    (.*).domain.com
RewriteRule ^/u/([^/]+)/?$ /user.php?user_name=$1&domain_given=%1 [P]

Remember how the LHS of the RewriteRule gets tested first. Then in the RewriteCond LHS gets matched against the RHS, and if this matches the substution will take place. Strings you capture in the RHS of a RewriteCond get stored in %1, %2 etc... Those you capture in the LHS of a RewriteRule in $+, ä2 etc...

share|improve this answer
    
Doesn't this keep the fact that the sub-domain is still there? I need it to execute domain.com/user.php?user_name=$1&domain_given=%1. I believe this answer just replaces the /u/satbirkira and would try to access the file subdomain.domain.com/user.php, which doesn't exists because there arn't really any subdomains or subdomain folders. I have a wildcard A type record *.domain.com pointing to the servers IP so that the server accepts the request in the hopes I can try to use a mod_rewrite. –  Satbir Kira Dec 11 '13 at 17:52
    
^ Scratch that, I talk to tech support, they added a wilcard subdomain which I didn't have. This works now! I though a wilcard A type record was enough. Thanks! Now domain.com/user.php can be accessed using anything.domain.com/user.php. I used shane's rewrite for htaccess since I'm not on a dedicated server and can't/don't want to deal with virualhost and httpd.config as such –  Satbir Kira Dec 11 '13 at 18:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.