Rewrite to pass directories, and query string as parameters

Using mod_rewrite to handle various content issues

Rewrite to pass directories, and query string as parameters

Postby macinjosh » Mon Feb 04, 2008 1:45 pm

Hey everybody,

I need to rewrite the following URL

example.com/directory1/directory2/page?id=1

into:

example.com/template.php?p=page&dir=/directory1/directory2&qp=?id=1

I've been struggling with writing a RewriteRule for this, and I'm not convinced that it is possible.

I realize it is a bit strange but it will be used for a templating system I'm working on for a project.

Any ideas?
macinjosh
 
Posts: 4
Joined: Mon Feb 04, 2008 1:36 pm

Postby richardk » Mon Feb 04, 2008 2:00 pm

You want the last part in p, everything before it in dir and the query string in qp?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^((.+)/)([^/]+)/?$ /template.php?dir=$2&page=$3&qp=%{QUERY_STRING} [L]


The query string part probably won't work, but you can set it in the PHP with
Code: Select all
$_GET['qp'] = getenv('QUERY_STRING');

and then remove
Code: Select all
&qp=%{QUERY_STRING}

from the mod_rewrite.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby macinjosh » Tue Feb 05, 2008 10:03 am

Wow, thanks, that is working quite well. One small issue though

a url like:

example.com/page

doesn't work.

it should return something like:

example.com/template.php?dir=&p=page

Any thoughts? Once again thanks a million!
macinjosh
 
Posts: 4
Joined: Mon Feb 04, 2008 1:36 pm

Postby richardk » Tue Feb 05, 2008 11:22 am

The first part needs to be optional
Code: Select all
^((.+)/)?([^/]+)/?$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby macinjosh » Tue Feb 05, 2008 12:13 pm

Yeah I tried that at first and i end up getting an Internal Server Error, but thanks for the suggestion
macinjosh
 
Posts: 4
Joined: Mon Feb 04, 2008 1:36 pm

Postby richardk » Tue Feb 05, 2008 12:51 pm

It's looping because the rule now matches template.php. Add these conditions before the rule
Code: Select all
# If it's not an existing file (eg. template.php) and
RewriteCond %{SCRIPT_FILENAME} !-f
# it's not an existing directory (you may not need this).
RewriteCond %{SCRIPT_FILENAME} !-d
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

.html issue

Postby zooloo14 » Mon Apr 14, 2008 12:26 pm

Hello Richard,

I have found this post very useful for my case. So I'm using:

Code: Select all
# If it's not an existing file (eg. template.php) and
RewriteCond %{SCRIPT_FILENAME} !-f
# it's not an existing directory (you may not need this).
RewriteCond %{SCRIPT_FILENAME} !-d

Options +FollowSymLinks

RewriteEngine On
RewriteRule ^((.+)/)?([^/]+)/?$ /template.php?dir=$2&page=$3&query=%{QUERY_STRING} [L]


it works fine for a file ending in .php but it does not redirect for files ending with .html.

I've tried adding AddType application/x-httpd-php .php .htm .html but it did not work.

Thanks in advance
zooloo14
 
Posts: 2
Joined: Mon Apr 14, 2008 12:18 pm

continued...

Postby zooloo14 » Mon Apr 14, 2008 12:43 pm

humm. sorry about the previous question. It seems to be a matter of an actual file already existing rather than the extension.

Is there a way that the Rewrite rule override a file that really exists?

For Example:

I would like that example.com/directory1/directory2/page.html

redirects to the template.php page (layout page)

which in turns include the actual example.com/directory1/directory2/page.html static file (content).
zooloo14
 
Posts: 2
Joined: Mon Apr 14, 2008 12:18 pm

Postby richardk » Mon Apr 14, 2008 2:24 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Ignore only template.php.
RewriteCond %{REQUEST_URI} !^/template\.php$ [NC]
RewriteRule ^((.+)/)?([^/]+)/?$ /template.php?dir=$2&page=$3&query=%{QUERY_STRING} [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Content

Who is online

Users browsing this forum: No registered users and 17 guests

cron