We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


htaccess RewriteRule regex

Medium Priority
Last Modified: 2012-05-06
I'm attempting to create a rewrite rule that will convert my url and redirect according to modrewrite structures...

i'm looking to take a url similar to this...


And convert it back to: http://www.somesite.com/index.php?page=123&active=true

Notice how I ditched everything before /page/......

I'm hoping this regex will work for unlimited number of name value pairs following that first /page/ entry. All of my urls will have that page item and everything following will be name value pairs.
RewriteRule ^pageid/([0-9]+)/nathan/true/$ /index.php?id=$1&nathan=true [NC]
Thats what I have currently, obviously doesn't come close :)

Open in new window

Watch Question

Top Expert 2008
> will work for unlimited number of name value pairs

You better do that in PHP (spilt a string against the slash) due to a problem with the N flag and path_info or don't use slashes (i.e. non-existent folders) in your URL to use a solution with the N flag.

RewriteEngine on
RewriteRule page/([0-9]+)/(.*) /index.php?page=$1&otherArgs=$2 [L]

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


any idea why it wouldn't work for pages that have a php redirect on them.

some of our pages /pageid/2/ for instance which is really (index.php?id=2) actually has a php redirect and goes to index.php?=6

The page just spins and never resolves.
Top Expert 2008

If you redirect via header('Location: http://example.com/index.php?=6.'), mod_rewrite can't do anything since this is the http response. You'll hale to change the php redirect, i.e. the location header to the desired URL like header('Location: http://example.com/page/6.')


i'm not concerned about the url display on the redirect, i'm just saying that the page never ends up anywhere if there is a php redirect on the page the htaccess points to. It never loads...

ie if you make your mod rewrite direct to somepage.php and then have a php redirect on that page to someotherpage.php it will never display the contents of the last page will never print out.

Top Expert 2008

Unless you redirect to a URL containing /page/digit/ the RewriteRule isn't involved in the second request so I'd look for a php issue.

If you redirect to http://example.com/index.php?=6  the file index.php will be served directly in the second request w/o anything done by the rewrite rule. This is a completely separate request from the first one, so there can't be a relation between
1st request --> rewrite /page/6 to index.php --> serve index.php -> index.php issued location header with http://example.com/index.php?=6

client stars a fresh request for the URl given in the location header: http://example.com/index.php?=6
2nd request -> index.php does not match rule-pattern --> serve index.php. If your application sends a location header again, it's a problem in the application.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.