views:

249

answers:

3

Hello all,

I am attempting clean useful URLs using mod_rewrite. I am sure this is a common question but I am not so hot with mod_rewrite:

I have this URL: http://mysite.com/user.php?user=fatcatmat&sort=popularv

I want to be able to rewrite it like this:

http://mysite.com/user/user/fatcatmat/sort/popularv (Is there a way to remove duplicates in a URL?)

I think I have managed to do the removal of the PHP extension.

RewriteRule ^(.*)\$ $1.php [nc]

Is the above correct?

For the separate pages, I would have something like this.

RewriteRule ^/?user(/)?$ user.php

Main Question: Its a bit tedious to do all the above but is there a MEGA rewrite rule that will just place "/" in between variables and their values and remove the .php extension from all pages?

Thank you for any help.

A: 

Try to use smth like this:

RewriteBase /
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)&(\w+)=(\w+)$ [NC]
RewriteRule ^user.php$ user/%1/%2/%3/%4? [NC,R=301,L]

RewriteRule ^user/(\w+)/(\w+)/(\w+)/(\w+)$ user.php?$1=$2&$3=$4 [L]
TonyCool
A: 

Try these rules:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ (/[^/?\s]+)+\.php[?/\s]
RewriteCond %{QUERY_STRING} ^([^/&=]+)=([^/&]+)(&([^&].*))?$
RewriteRule \.php$ ?%4 [N,E=PATH:%{ENV:PATH}/%1/%2]
RewriteCond %{ENV:PATH} ^/
RewriteRule \.php$ %{ENV:PATH} [L,R=301]

RewriteRule ^(.*)/([^/]+)/([^/]+)$ $1?$2=$3 [N,QSA]
RewriteRule ^([^/]+)/([^/]+)$ $1.php?$1=$2 [L,QSA]

But it would be a lot easier to do that with PHP: Check what URI path has been requested (see $_SERVER['REQUEST_URI']) and redirect if necessary (see header function).

Gumbo
A: 

You can create rewrite rules from this website on the fly Mod_rewrite generator

halocursed