Forum Moderators: phranque

Message Too Old, No Replies

Rewrite file extension as variable

         

optik

5:35 pm on Jul 3, 2011 (gmt 0)

10+ Year Member



I want to add a rewrite rule that will change xyz.xml to xyz.html?type=xml

any ideas if this can be done?

g1smd

5:41 pm on Jul 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There's somewhere close to 5000 previous threads with examples of how to do this.

Are both of those URLs as used out on the web? Or is one a URL used on the web and the other a filename and parameters used only inside the server?

The answer dictates whether you need a redirect or a rewrite. Both use RewriteRule syntax. One uses the [R=301,L] flags and the other only the [L] flag.

optik

6:08 pm on Jul 3, 2011 (gmt 0)

10+ Year Member



it's ok I have it

RewriteRule ^(.*)\.(xml) $1.html?$3&type=xml [L]

g1smd

6:13 pm on Jul 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Several Errors.

The (.*) is the WRONG patttern as it matches the whole request then has to "back off and retry" hundreds of times to find out what you actually meant.

You need to add a leading slash before $1 unless you want to expose your site to some serious hacks.

$3 is undefined. Only two backreferences are created from the RegEx pattern.

optik

1:57 pm on Jul 17, 2011 (gmt 0)

10+ Year Member



how does this look

RewriteCond %{QUERY_STRING} (.*)?
RewriteRule ^(dir/)(.*)\.(xml) /new_dir/$2.html?%1&changed=true [L]

g1smd

4:39 pm on Jul 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Many small changes, every one for good reason.

RewriteCond %{QUERY_STRING} .?
RewriteRule ^dir/([^.]+)\.xml$ /new_dir/$1.html?changed=true [QSA,L]

optik

9:08 am on Jul 18, 2011 (gmt 0)

10+ Year Member



ok thanks, one day I'll understand mod-rewrite

optik

9:11 am on Jul 18, 2011 (gmt 0)

10+ Year Member



actually one thing that's missing is I need the variables passed to the new string like I had here html?%1&changed=true

lucy24

3:23 pm on Jul 18, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's what QSA is for. It stands for "query string append" and means add "changed=true" to the existing query string instead of replacing it with "changed=true".

By default, nothing that happens in a rewrite affects the query string. To change the query you have to do one of three things:

if the target ends in ? alone, it means delete the existing query

if the target ends in ?{morestuff} it means replace the existing query with {morestuff}, or create a new query if it wasn't there before

if the target ends in ?{morestuff} and you say [QSA] it means add {morestuff} to the existing query

The braces {} are not part of the Regular Expression, they are just my notation.

optik

8:42 am on Jul 19, 2011 (gmt 0)

10+ Year Member



Ok thanks, that's something to remember