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>
8
  • 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. Commented Jun 21, 2014 at 13:58
  • @Hakre can you pls tell me how its possible using route handler Commented Jun 21, 2014 at 14:14
  • which route handler are you using? Commented Jun 21, 2014 at 14:15
  • @hakre I am not using any route handler but i need one Commented Jun 21, 2014 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 - stackoverflow.com/q/768431/367456 Commented Jun 21, 2014 at 14:37

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.