views:

25

answers:

2

I am trying to rewrite a URL using .htaccess. I am running on Apache version 2.2.15.

Contents of .htaccess:

RewriteEngine on
RewriteRule cars/(.*) cars/member_page.php?user=$1
RewriteRule cars/(.*)/ cars/member_page.php?user=$1

Contents of member_page.php:

<?php
echo $_GET[user];
?>

URL entered into browser:

http://www.mydomain.com/cars/user1

The browser outputs the string "member_page.php" instead of "user1"

How do I make it output the contents of (.*) from the original URL.

A: 

Well, first of all, its $_GET['user'], not $_GET[user] (note the quotes). Regarding your mod_rewrite Rules:

RewriteEngine On
RewriteRule cars/(.*) cars/member_page.php?user=$1 [L]
RewriteRule cars/(.*)/ cars/member_page.php?user=$1 [L]

L means last rule to be applied.

middus
But a 302 redirect is probably not what he wants, is it? Won't the [L] already do?
Pekka
Just realised that, too and edited my answer accordingly.
middus
A: 

The problem with this format:

RewriteEngine on
RewriteRule cars/(.*) cars/member_page.php?user=$1 [L]
RewriteRule cars/(.*)/ cars/member_page.php?user=$1 [L]

Turns out that I was rewriting cars/whatever to cars/member_page.php. This creates the problem of rewriting to the same directory that the rewrite is on.

So

cars/user1

was rewritten to

cars/member_page.php/user=member_page.php

It was rewritten twice using the content from the second rewrite as the final information. The solution is to move member_page.php to a different directory.

RewriteEngine on
RewriteRule cars/(.*) memberscars/member_page.php?user=$1 [L]
RewriteRule cars/(.*)/ memberscars/member_page.php?user=$1 [L]
Mark