You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Nick Tkach <nt...@gmail.com> on 2011/10/03 20:25:19 UTC

[users@httpd] Vanity URL Rewrites Best Practices?

Does anyone have any pointers/suggestions on the best way to do vanity url
rewrites?

For example,



http://foo.com/mmh/maintenance_plan/tip?contentCategoryType=MaintenanceTip&id=%2Fwww%2Favm_webapps%2Fmmh%2Fmaintenance-tips%2Fcontent%2Fafter_blizzard.xml


Being sent to


 http://foo.com/mmh/articles/authored/after-blizzard


We've got a very frequent process where we'll get a huge block of rewrites
like this that vary just by the last part and so far just keep going through
and adding dozens and dozens of new rewrite rules each time.  Surely there
has to be a better way?  (Ideally that just involve Apache changes and not
code changes on the back-end)

Re: [users@httpd] Re: Vanity URL Rewrites Best Practices?

Posted by Tom Evans <te...@googlemail.com>.
On Thu, Oct 6, 2011 at 3:00 PM, Nick Tkach <nt...@gmail.com> wrote:
> Just to answer my own question, in case it helps someone else down the
> road, what I was missing was that inside the curly braces you need to
> dereference the variable with % rather than $.  So the right version
> of what I posted above is:
>
>  RewriteMap vanmap txt:/tmp/map.txt
>
>  RewriteCond %{REQUEST_URI} ^/mmh/
>  RewriteCond %{QUERY_STRING} \%2F(\w+)\.xml$
>
>  RewriteRule .* /mmh/${vanmap:%1}? [L,NC,R=302]
>

Since this is for posterity, I have to say 'No!'.

In both the RewriteRule and the RewriteCond, %N refers to back
references in the last matched RewriteCond, and $N refers to back
references in the RewriteRule. It doesn't matter one jot that it is
'inside the curly braces', it matters where the data comes from.

This is mentioned in both the RewriteCond and the RewriteRule documentation:

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule

Cheers

Tom

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Re: Vanity URL Rewrites Best Practices?

Posted by Nick Tkach <nt...@gmail.com>.
On Wed, Oct 5, 2011 at 2:48 PM, Nick Tkach <nt...@gmail.com> wrote:
> On Mon, Oct 3, 2011 at 1:41 PM, Andrew Schulman
> <an...@alumni.utexas.net> wrote:
>>
>> > For example,
>> >
>> > http://foo.com/mmh/maintenance_plan/tip?contentCategoryType=MaintenanceTip&id=%2Fwww%2Favm_webapps%2Fmmh%2Fmaintenance-tips%2Fcontent%2Fafter_blizzard.xml
>> >
>> > Being sent to
>> >
>> >  http://foo.com/mmh/articles/authored/after-blizzard
>> >
>> > We've got a very frequent process where we'll get a huge block of rewrites
>> > like this that vary just by the last part and so far just keep going through
>> > and adding dozens and dozens of new rewrite rules each time.  Surely there
>> > has to be a better way?  (Ideally that just involve Apache changes and not
>> > code changes on the back-end)
>>
>> Are the requests similar enough that you can write a single regular expression,
>> or maybe two or three, that extracts the useful part from the URL in every case?
>> For example,
>>
>> RewriteCond %{REQUEST_URI} ^/mmh/
>> RewriteCond %{QUERY_STRING} \%2F(\w+)\.xml$
>> RewriteRule .* /mmh/articles/authored/%1
>>
>> If you can describe all of the requests in this way or something like it, you're
>> done.  If not, if each request is so different that it needs its own regular
>> expression, then it seems you're doomed to keep doing it as you are now.
>
> Yes, I'm very nearly there, thanks!  The only thing is, I'm not quite
> sure how to combine what you have here with a RewriteMap.  If I had
> something like this:
>
> RewriteMap vanmap txt:/tmp/map.txt
>
> RewriteCond %{REQUEST_URI} ^/mmh/
> RewriteCond %{QUERY_STRING} \%2F(\w+)\.xml$
>
>
> Then can I just do a rewrite rule at the end like this?
>
> RewriteRule .* /mmh/${vanmap:$1}? [L,NC,R=302]
>
>
> Because I tried that and it keeps failing to pull any kind of key out
> of the REQUEST_URI.
>
>>
>> Good luck,
>> Andrew.
>>
>>
>> ---------------------------------------------------------------------
>> The official User-To-User support forum of the Apache HTTP Server Project.
>> See <URL:http://httpd.apache.org/userslist.html> for more info.
>> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>>   "   from the digest: users-digest-unsubscribe@httpd.apache.org
>> For additional commands, e-mail: users-help@httpd.apache.org
>>
>

Just to answer my own question, in case it helps someone else down the
road, what I was missing was that inside the curly braces you need to
dereference the variable with % rather than $.  So the right version
of what I posted above is:

 RewriteMap vanmap txt:/tmp/map.txt

 RewriteCond %{REQUEST_URI} ^/mmh/
 RewriteCond %{QUERY_STRING} \%2F(\w+)\.xml$

 RewriteRule .* /mmh/${vanmap:%1}? [L,NC,R=302]

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Re: Vanity URL Rewrites Best Practices?

Posted by Nick Tkach <nt...@gmail.com>.
On Mon, Oct 3, 2011 at 1:41 PM, Andrew Schulman
<an...@alumni.utexas.net> wrote:
>
> > For example,
> >
> > http://foo.com/mmh/maintenance_plan/tip?contentCategoryType=MaintenanceTip&id=%2Fwww%2Favm_webapps%2Fmmh%2Fmaintenance-tips%2Fcontent%2Fafter_blizzard.xml
> >
> > Being sent to
> >
> >  http://foo.com/mmh/articles/authored/after-blizzard
> >
> > We've got a very frequent process where we'll get a huge block of rewrites
> > like this that vary just by the last part and so far just keep going through
> > and adding dozens and dozens of new rewrite rules each time.  Surely there
> > has to be a better way?  (Ideally that just involve Apache changes and not
> > code changes on the back-end)
>
> Are the requests similar enough that you can write a single regular expression,
> or maybe two or three, that extracts the useful part from the URL in every case?
> For example,
>
> RewriteCond %{REQUEST_URI} ^/mmh/
> RewriteCond %{QUERY_STRING} \%2F(\w+)\.xml$
> RewriteRule .* /mmh/articles/authored/%1
>
> If you can describe all of the requests in this way or something like it, you're
> done.  If not, if each request is so different that it needs its own regular
> expression, then it seems you're doomed to keep doing it as you are now.

Yes, I'm very nearly there, thanks!  The only thing is, I'm not quite
sure how to combine what you have here with a RewriteMap.  If I had
something like this:

RewriteMap vanmap txt:/tmp/map.txt

RewriteCond %{REQUEST_URI} ^/mmh/
RewriteCond %{QUERY_STRING} \%2F(\w+)\.xml$


Then can I just do a rewrite rule at the end like this?

RewriteRule .* /mmh/${vanmap:$1}? [L,NC,R=302]


Because I tried that and it keeps failing to pull any kind of key out
of the REQUEST_URI.

>
> Good luck,
> Andrew.
>
>
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>   "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
>

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


[users@httpd] Re: Vanity URL Rewrites Best Practices?

Posted by Andrew Schulman <an...@alumni.utexas.net>.
> For example,
> 
> http://foo.com/mmh/maintenance_plan/tip?contentCategoryType=MaintenanceTip&id=%2Fwww%2Favm_webapps%2Fmmh%2Fmaintenance-tips%2Fcontent%2Fafter_blizzard.xml
> 
> Being sent to
> 
>  http://foo.com/mmh/articles/authored/after-blizzard
> 
> We've got a very frequent process where we'll get a huge block of rewrites
> like this that vary just by the last part and so far just keep going through
> and adding dozens and dozens of new rewrite rules each time.  Surely there
> has to be a better way?  (Ideally that just involve Apache changes and not
> code changes on the back-end)

Are the requests similar enough that you can write a single regular expression,
or maybe two or three, that extracts the useful part from the URL in every case?
For example,

RewriteCond %{REQUEST_URI} ^/mmh/
RewriteCond %{QUERY_STRING} \%2F(\w+)\.xml$
RewriteRule .* /mmh/articles/authored/%1

If you can describe all of the requests in this way or something like it, you're
done.  If not, if each request is so different that it needs its own regular
expression, then it seems you're doomed to keep doing it as you are now.

Good luck,
Andrew.


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Vanity URL Rewrites Best Practices?

Posted by Scott Gifford <sg...@suspectclass.com>.
On Mon, Oct 3, 2011 at 2:25 PM, Nick Tkach <nt...@gmail.com> wrote:

> Does anyone have any pointers/suggestions on the best way to do vanity url
> rewrites?
>
> For example,
>
>
>
> http://foo.com/mmh/maintenance_plan/tip?contentCategoryType=MaintenanceTip&id=%2Fwww%2Favm_webapps%2Fmmh%2Fmaintenance-tips%2Fcontent%2Fafter_blizzard.xml
>
>
> Being sent to
>
>
>  http://foo.com/mmh/articles/authored/after-blizzard


Drupal <http://drupal.org/> uses rules like this:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !=/favicon.ico
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

That is, "if there is no matching file or directory, redirect to an
index.php script and pass the URL in as the parameter q".  That script then
looks up the URL in a database and serves up the right content.

That is a pretty common approach.

Hope this helps,

-----Scott.