Forum Moderators: phranque

Message Too Old, No Replies

RedirectMatch with variables

RedirectMatch with variables

         

esprague

9:11 pm on Mar 22, 2006 (gmt 0)

10+ Year Member



We need to use RedirectMatch to physically change the url in the address bar.
When there are no arguments everything works fine but we need it to work with parameters.

A simple example that works on our site is as follows:
RedirectMatch permanent ^/x.html$ [oursite.com...]
In this case when someone types
[oursite.com...]
it physically changes the url address to
[oursite.com...]

We need to get this working with pages that have arguments.
In other words we need it to work such that if someone types in
[oursite.com...]
it physically changes the url address to
[oursite.com...]

I've tried
RedirectMatch permanent ^/my.php?z=(.*)$ [oursite.com...]
but it does not work.

The [webmasterworld.com...] post was helpful, bobriggs explains that the parameter is in the environment as the QUERY_STRING, that it is not considered part of the URL. This explains why we can't get it to work. He mentions that one could change the environment with a combination of redirectMatch (or mod_rewrite) and the SetEnvIf directive. However, there are no specific examples and I'm not having much luck.

Note that this is different from a RewriteRule where one page is treated as if it is another. In our case the key is that the address phycially changes.

Thanks,
Eric

Birdman

10:08 pm on Mar 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's an example :)

RewriteCond %{QUERY_STRING} ^z=(.*)$
RewriteRule ^my\.php$ [localhost...] [L,R=301]

That should get you going. You can change the "R" code to what you want.

PS: Welcome to Webmaster World!

esprague

10:51 pm on Mar 22, 2006 (gmt 0)

10+ Year Member



Birdman,

Thanks for the welcome to webmasterworld, I'm a long-time reader but first-time poster :)

Thanks for the example but the url still is not physically changing.

It is true that when the user types in
[oursite.com...]
the contents of the
[oursite.com...] page are now displayed instead.

However, the address bar on top still shows [oursite.com...] as the url and we need it to display [oursite.com...] as the url instead.

Thanks,
Eric

Birdman

11:49 pm on Mar 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Couldn't you just change the links in your site to point to the "good" URL? (ie. /33.htm)

Then, use mod_rewrite to send the request to your script silently.

My question for you is:

What are you trying to achieve? Is this SEO related or just for user purposes.

Cheers

esprague

12:02 am on Mar 23, 2006 (gmt 0)

10+ Year Member



Birdman,

The short answer is that we think this is the easiest approach.

The long answer is that these links are created programatically so we would have to hack into quite a bit of code to change the links to the "good" url. The reason the "good" url addresses work even though the program generates the "bad" url addresses is that we have another program that makes the "good" url addresses work. Rather than hack into the first program we want to just handle this through the .htaccess file.

We are doing this for SEO reasons and for adsense reasons.

Thanks,
Eric

Birdman

1:18 am on Mar 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For SEO purposes, the only way is to alter the hard links. It's all about what they see.

esprague

4:02 am on Mar 23, 2006 (gmt 0)

10+ Year Member



True, if this was strictly for SEO then all that matters is what they see.

We are also doing this for adsense. Our adsense ads seem to be more targeted on pages with .htm or .html extensions than on our .php pages. In fact we've done testing where we have the exact same content on a new .htm page as on a new .php page. Both pages are new so Google has not crawled either one. If a user with the Google toolbar refreshes the .htm page a few times then the ads become targeted whereas if a user refreshes the .php page a few times the ads stay generic.

jdMorgan

5:00 pm on Mar 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's what most people need to do:

  • Replace all dynamic (.php?z=33) links appearing on pages with static (33.htm) links -- either by editing the database, or by using preg_replace in the PHP code.
  • Internally rewrite incoming requests for those static links to the dynamic form needed to invoke the script(s) on your site.
  • Externally redirect direct client requests for dynamic links to the equivalent static URL.

    Note that mod_rewrite does not change the content of your pages. It takes action only on incoming requested URLs, and either changes the file associated with that requested URL or performs an external redirect to a new URL, depending on the specified RewriteRule syntax. This rewriting and/or redirecting takes place before any content is served and before any content-handlers or scripts are invoked.

    Another extremely common misperception is that of the 'direction' of a rewrite; Many people see it backwards. The comments in the code below are technically-correct.

    Here's a simple single-variable example of the second two of the above-described three steps:


    # Rewrite static URL requests to my.php script
    RewriteRule ^([0-9]+)\.htm$ /my.php?z=$1 [L]
    #
    # Redirect direct client requests for dynamic URLs to static equivalent
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /my\.php?z=([0-9]+)
    RewriteRule ^my\.php$ http://%{HTTP_HOST}/%1.htm? [R=301,L]

    It is necessary to use the somewhat-redundant-looking rule with the THE_REQUEST variable to prevent these two rules from creating an 'infinite' rewrite/redirect loop.

    Jim

  • esprague

    7:51 pm on Mar 23, 2006 (gmt 0)

    10+ Year Member



    We got it working as follows:
    RewriteCond %{QUERY_STRING} ^z=(.*)$
    RewriteRule ^my\.php$ [oursite.com...] [R]

    The url physically changes in the address bar now that we have the [R] in place.

    Can we limit the query string so that this rewrite only takes place when z=x is the only argument?

    In other words we want
    [oursite.com?my.php?z=33...]
    to redirect to
    [oursite.com...]

    but we want
    [oursite.com?my.php?z=33&name=fred...]
    to stay as
    [oursite.com?my.php?z=33&name=fred...]
    because the z argument is not the only argument.

    jdMorgan

    8:05 pm on Mar 23, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Use a specific pattern to match one or more numbers (only), and end-anchor that pattern:

    RewriteCond %{QUERY_STRING} ^z=([0-9]+)$

    Jim

    esprague

    11:29 pm on Mar 23, 2006 (gmt 0)

    10+ Year Member



    Jim,

    Thanks for the tip, it now behaves exactly the way we need it to when other arguments are in the url.

    We want to keep this rewrite in the root .htaccess file but the files in question are in a subdirectory. I didn't think it was a big deal before but I can't get it to work in the subdirectory.

    In other words, we want it to apply to
    [oursite.com...]
    type files. I've tried a few things but can't get it to go.

    Thanks Again,
    Eric

    jdMorgan

    11:47 pm on Mar 23, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Change the pattern to detect requests for that file in the subdirectory, and change the substitution URL to redirect to the new file in that subdirectory:

    RewriteRule ^[b]path_to_subdir/[/b]my\.php$ http://www.example.com/[b]path_to_subdir/[/b]%1.htm? [R]

    This is all about matching a request, and rewriting it or redirecting it someplace.

    I'd also suggest that you use [R=301,L] to specify an immediate 301-Moved Permanently redirect, unless this is really only a temporary change.

    Jim

    esprague

    12:02 am on Mar 24, 2006 (gmt 0)

    10+ Year Member



    Jim,

    Thanks for the subdirectory syntax, this is working nicely now :)