Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


htaccess RewriteRule regex

Posted on 2009-02-12
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

Question by:phenixfilms
  • 3
  • 2
LVL 27

Accepted Solution

caterham_www earned 2000 total points
ID: 23625359
> 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


Author Comment

ID: 23626446
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.
LVL 27

Expert Comment

ID: 23626567
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.')

Author Comment

ID: 23626699
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.

LVL 27

Expert Comment

ID: 23626872
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.

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question