tags:

views:

14

answers:

1

As the title says, how can I use .htaccess and mod_rewrite to change this?

"pages/123/some-text" to "showpage.php?index=123&title=some-text"

I have started with this:

RewriteEngine On
Options +FollowSymLinks 
RewriteBase /

RewriteCond %{REQUEST_FILENAME} pages/(.*)
RewriteRule (.*) /showpage.php?index=%1 [R]

Which allows me to atleast get the parameter for index, but this fails unless I create an empty directory called pages.

+1  A: 

You can just extract the relevant parts of the URL in the RewriteRule itself:

RewriteEngine On
Options +FollowSymLinks 
RewriteBase /

RewriteRule ^pages/([^/]+)/(.+)$ /showpage.php?index=$1&title=$2 [R]

Note that the R flag causes an external redirect, is this what you want? If not, you should remove it:

RewriteRule ^pages/([^/]+)/(.+)$ /showpage.php?index=$1&title=$2

Or, if you do want an external redirect, to be safe, it's a good idea to use the L flag so the redirect is applied immediately (and add the 301 status if the redirect is permanent):

RewriteRule ^pages/([^/]+)/(.+)$ /showpage.php?index=$1&title=$2 [R=301,L]

Also, if you want the title piece to be optional, you can do that too:

RewriteCond &title=$3 ^(&title=.+)$
RewriteRule ^pages/([^/]+)(/(.+))?$ /showpage.php?index=$1%1
Tim Stone
Yes, I want the external redirect instead of internal so that links on the page are relative to / and not /pages/ from the browser's perspective. Thanks!
Dmi