Forum Moderators: phranque
Anyway, here's what I'm looking for. I have a subdomain that is going to contain a shopping cart/ordering system we're going to use for multiple users, so I want it to take:
shop.domain.com/USER_NAME/
and go to
shop.domain.com/index.php?user=USER_NAME
I got that part working correctly... however, I need some pages in there to work too.
so:
shop.domain.com/USER_NAME/shoppingcart.php
would go to
shop.domain.com/shoppingcart.php?user=USER_NAME
Here's what I have so far (note that I took it from other places and repurposed it for my own, so I might have some unnecessary parts):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTP_HOST} ^store.domain.com$
RewriteRule ^.*$ - [S=40]
RewriteRule ^([^/]+)/?([0-9]+)?/?$ /index.php?user=$1 [QSA,L]
</IfModule>
Welcome to WebmasterWorld!
> Here's what I have so far (note that I took it from other places and repurposed it for my own, so I might have some unnecessary parts):
There are some glaring inconsistencies in that code, so --in the friendliest possible way-- I need to warn you that it's a very bad idea to cut-n-paste code into your server configuration files unless you understand every single character in that code.
For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].
These resources will help you understand what you've got, and to identify the details that we'll need to see in order to provide meaningful input on your project.
Jim
Thanks for the response. The reason I hacked it together like that was because one of the only places I'd really seen Mod Rewrite so far was in Word Press. So I looked at their stuff, and then tried to repurpose it.
I took a brief look at some of the other articles trying to get a basic grasp on it so far. One of the problems I think is my weakness in the area of regular expressions.
Anyway, I'm working on rewriting that, and I was wondering one thing in particular, If I'm going to be rewriting several pages, do I need to specify them all (which seems bulky, but at the same time, would only allow access to those specific files), or should the mod rewrite handle all pages?
In the form:
store.domain.com/user to store.domain.com/index.php?user=user
store.domain.com/user/shoppingcart.php to store.domain.com/shoppingcart.php?user=user
and so on...
I know this is supposed to be more of a discussion board than a help desk, but I am just trying to get this done ASAP to try and meet a deadline.
As for the actual mod_rewrite code... can you point me to a good link on the brackets at the end of the line ([QSA,L]), I know there are various things that can go there, I didn't run across anything detailing them though.
-Patrick
After looking into it some more, here's what I came up with. The problem is, that its rewriting each specific page I need, so if more pages were added, then they would have to be added here. Then again, maybe that's a good practice security-wise?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTP_HOST} ^store.domain.com$
RewriteRule ^([^/]+)/$ /index.php?user=$1 [QSA,L]
RewriteRule ^([^/]+)/shoppingcart\.php$ /shoppingcart.php?user=$1 [QSA,L]
RewriteRule ^([^/]+)/merch\.php$ /merch.php?user=$1 [QSA,L]
RewriteRule ^([^/]+)/checkout\.php$ /checkout.php?user=$1 [QSA,L]
</IfModule>
To do all the pages would be:
RewriteRule ^([^/]+)/([^/.?])\.php$ /$2.php?user=$1 [QSA,L]
Does that seem about right?
RewriteRule ^([^/]+)/([^/.?])\.php$ /$2.php?user=$1 [QSA,L]
to
RewriteRule ^([^/]+)/([^/\.?])\.php$ /$2.php?user=$1 [QSA,L]
What about the "?"?
Or is there a better RegEx for getting a page name. It needs to include all upper and lower case letters, numbers, and "_".... so ([a-z][A-Z][0-9]_) perhaps?
Thanks for the help.
If you don't understand these patterns, then I'd recommend some more time learning about regular expressions. Every single character in your regex must be correct, or you run the risk of unintended -and sometimes serious- consequences.
The pattern ([^/]+)/([^/.?]) means "One or more characters not equal to a slash, followed by a slash, followed by a single character not equal to a slash, a period, or a question mark." As such, it won't meet your needs unless you change the second part of the pattern to ([^/.]+). This allows for more than a single character, and eliminates the question mark, since it will never appear the in string tested by RewriteRule (query strings must be processed separately, using a RewriteCond %{QUERY_STRING} directive).
Jim