views:

150

answers:

3
+2  Q: 

URL Mod-Rewrite

Hello,

I currently have a working URL:

http://example.com/security-services.php?service=fixed-camera-surveillance

and then I have PHP say, $_REQUEST['service'] to do some stuff...

But I'd like to achieve the same function if the URL looked like this:

http://example.com/security-services/fixed-camera-surveillance

Thanks!

A: 

You can create a rule like this:

RewriteRule ^security-services/(.*)/? /security-services.php?service=$1

If you're using a .conf file, change ^ to ^/

Greg
+3  A: 

An .htaccess file with something like this should do it.

Options +FollowSymLinks
RewriteEngine On

RewriteRule security-services/(.*)/? security-services.php?service=$1 [L]

The part that says security-services/(.*)/? matches the URL in the browser and rewrites it to security-services.php.

The key part is the (.*) which captures that portion of the URL and passes it to the PHP script as a GET value.

Mark Biek
Thanks for replying!I've done what you've suggested and the page loads fine. The only problem I'm facing is capturing the 'service' variable.Let's say I want to test it, on the original example I'd have:http://example.com/security-services.php?service=fixed-camera-surveillanceand could test with "echo $_REQUEST['service']"New example,http://example.com/security-services/fixed-camera-surveillanceI want "fixed-camera-surveillance' to be echo'd.Suggestions?
Mike B.
Have you tried it? the code above does it, it takes anything it finds with the `(.*)` regex and dumps it where you see the `$1`, so your script at the `security-services.php` file.
MalphasWats
Yes, I've tried it. I want to capture 'fixed-camera-surveillance' from the URL '.../security-services/fixed-camera-surveillance'. Would I be asking for ['service'] as before, or is it just stored in $1?
Mike B.
The first group of () is $1, the second $2, and so on. So you can capture multiple things and then use them in the rewrite part of the rule.
Mark Biek
Thanks Mark for getting back to me. I'm sorry; either I'm misunderstanding you, or you to me (probably to former). I've implemented your solution, as is. Could you *literally* write a PHP command to get the contents of /.../ (or $1 as you've described). <? echo $_REQUEST['services']; ?> is not doing the trick. Thanks!
Mike B.
**Edit to last sentence in last comment » <? echo $_REQUEST['service']; ?>
Mike B.
it should, although I prefer <?php echo $_GET['service']; ?> just to check all the boxes, your file is called `.htaccess` and it is uploaded into either the folder you're working in or the root folder. Also, can you confirm that the `security-services.php` file is actually being run, it's just not receiving the value? or is nothing happening?
MalphasWats
Like MalphasWats says, $_GET['service'] should do the trick. You might add a print_r($_GET); just to see what the contents of $_GET actually are.
Mark Biek
It was $_GET all along! Rookie mistake. Thanks everyone!
Mike B.
@Stack_Overflow, reviewing comments to an answer is very cumbersome. If there are 6+ comments to a particular answer (or as I call it, a discussion) the most recent comments are hidden until someone clicks "add/show X more comments." Just wanted to share some usability feedback.
Mike B.
Alright, I goofed up when I said $_GET got it to work. It's not. To illustrate, I'm set up a print_r($_GET) and storing that output in <meta name="stackoverflowOutput"> at the bottom of the <head>, so you all can see. Here is the working example » http://tr.im/msex1, here is the desired, non-working example » http://tr.im/msex2
Mike B.
My working, .htaccess is located in the site root (/) and looks like this,Options +FollowSymLinksRewriteEngine OnRewriteRule ^security-services/(.*) /security-services.php?service=$1 [L]
Mike B.
+1  A: 

Try this rule:

RewriteEngine on
RewriteRule ^security-services/([^/]+)$ security-services.php?service=$1 [L]

The [^/]+ describes the next path segment after /security-services, (…) forms a group that’s match then can referenced to with $1 in the substitution.

And if you want a more general for any kind of …-service.php file:

RewriteEngine on
RewriteRule ^([^/-]+)-services/([^/]+)$ $1-services.php?service=$2 [L]
Gumbo
Thanks for replying!I've done what you've suggested and the page loads fine. The only problem I'm facing is capturing the 'service' variable.Let's say I want to test it, on the original example I'd have:http://example.com/security-services.php?service=fixed-camera-surveillanceand could test with "echo $_REQUEST['service']"New example,http://example.com/security-services/fixed-camera-surveillanceI want "fixed-camera-surveillance' to be echo'd.Suggestions?
Mike B.
@Mike B.: That should work the same way. You shouldn’t notice any difference.
Gumbo