2

I am pretty new to .htaccess rewrites, and I'm trying to create rules to dynamically rewrite the URLs.

For example, say the user enters the following URL:

http://example.com/xxx/?user=2002

It would be rewritten into:

http://example.com/xxx/user/2002/

If user passes multiple parameters like this:

http://example.com/xxx/?user=2002&activity=100&result=true

It should become:

http://example.com/xxx/user/2002/activity/100/result/

Note: All the query strings will be dynamically generated.

This is what I have come up with:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /news/

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /news/index.php [L]
</IfModule>

Update

I tried to make the above code and query string rewrite code to work together. The modified .htaccess looks like below:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /news/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /news/index.php [L]

# first take out query string from your /xxx/ URLs
RewriteCond %{QUERY_STRING} ^.+$
RewriteRule ^news/?$ %{REQUEST_URI}/%{QUERY_STRING}? [L]

# now convert & to /
RewriteRule ^([^&]+)&(.*)$ $1/$2 [L]

# now convert = to /
RewriteRule ^([^=]+)=([^=]+=.*)$ $1/$2 [L]    
RewriteRule ^([^=]+)=([^=]+)$ $1 [L,R]

# internal rule to replace /n1/v1/n2/v2 to QUERY_STRING
RewriteRule "^(news)/([^/]+)/([^/]*)(/.*)?$" /$1$4?$2=$3 [L,QSA]
</IfModule>
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Varun Sridharan
  • 1,983
  • 2
  • 20
  • 54
  • Just a little question: Why don't you do the redirect in PHP? That would keep your .htaccess clean, `index.php` is accessed earlier and it does the routing anyway so it looks like the more fitting place to me. – hakre Jun 21 '14 at 13:58
  • @Hakre can you pls tell me how its possible using route handler – Varun Sridharan Jun 21 '14 at 14:14
  • which route handler are you using? – hakre Jun 21 '14 at 14:15
  • @hakre I am not using any route handler but i need one – Varun Sridharan Jun 21 '14 at 14:22
  • Then it's easy: Check for the query string in PHP (it's in `$_SERVER`) and if present create the correct URL out of it, then redirect - http://stackoverflow.com/q/768431/367456 – hakre Jun 21 '14 at 14:37
  • This is also the first step in creating your own router as such one does reading those variables and then parses them, runs stuff based on it - like the redirect. – hakre Jun 21 '14 at 14:43
  • @Hakre i know that but is there any predefine / created script for route handler – Varun Sridharan Jun 21 '14 at 15:17
  • puh, I bet there are thousand scripts, however as you already know pretty much what you want and you have already the specifics of your redirect requirements, it's perhaps best to write one your own and / or add some libraries that support you with that. e.g. here is one router: https://github.com/chriso/klein.php – hakre Jun 21 '14 at 15:31

1 Answers1

6

Really tricky rules these are. Put these recursive rules in your root .htaccess:

RewriteEngine On
RewriteBase /news/

# first take out query string from your URLs
RewriteCond %{THE_REQUEST} \?\S+
RewriteRule ^/?$ %{QUERY_STRING}? [L]

# now convert all & to /
RewriteRule ^([^&]+)&(.*)$ $1/$2 [L]

# now convert all = to /
RewriteRule ^([^=]+)=([^=]+=.*)$ $1/$2 [L]
RewriteRule ^([^=]+)=([^=]+)$ $1/$2 [L,R]

# finally an internal rule to replace /n1/v1/n2/v2 to QUERY_STRING
RewriteRule "^([^/]+)/([^/]*)(?:/(.*))?$" $3?$1=$2 [L,QSA]

## Your existing stuff followed now
RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643