Forum Moderators: phranque

Message Too Old, No Replies

Need help with rewrite directive

how do I redirect html files to a query string

         

h0olapet

8:43 pm on Sep 10, 2005 (gmt 0)



Group,

Here is my issue:

I have a subdirectory of .html files (e.g. http://www.example.com/reviews/somereview.html)

I want to redirect requests for these files to a php query (e.g. http://www.example.com/reviews.php?page=somereview.html)

What is the correct syntax using the rewrite command to accomplish this? I appreciate everyone's help!

Best

Chris

[edited by: jdMorgan at 10:19 pm (utc) on Sep. 10, 2005]
[edit reason] Example.com [/edit]

jd01

9:42 am on Sep 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Chris,

Welcome to WebmasterWorld.

We do not normally code on demand, so let me point you in a direction and get you started, then you can ask some more specific questions about the working of things and we will help you out.

I am not sure if you need this for all .html files, a specific directory, etc, so I cannot give you an exact answer...

Here is an example that will give you a start:

This will redirect the entire directory to the php script:
RewriteEngine on
RewriteRule ^reviews/(.*)$ http://www.example.com/reviews.php?page=$1 [R=301,L]

This one will only redirect files ending in .html
RewriteRule ^reviews/([^.]+\.html)$ http://www.example.com/reviews.php?page=$1 [R=301,L]

They both say if the requested URL starts with reviews followed by a slash:

Rule 1:
() = back reference (variable)
.(dot) = any character except the end of a line
* = 0 or more of the preceding characters or group of characters.
So (.*) = store everything, after the /

$1 = everything that was stored

Rule 2:
() = See Above
[] = class definition (indicates grouping)
^ = not
.(dot) = inside a class definition, .(dot) means a litteral .(dot) not 'any character except the end of a line'.
+ = 1 or more of the preceding characters or group of characters.
\ = escaping character use to a match .(dot) (or other meta characters) litterally.

So [^.]+ = match anything that is not a .(dot) AND is followed by .html at the end of the line.

This should get you started. There is some more information in the Library. (Links at the top left of the pages.)

Justin