Forum Moderators: phranque
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
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!
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
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
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.
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]
Jim
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.
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
RewriteRule ^[b]path_to_subdir/[/b]my\.php$ http://www.example.com/[b]path_to_subdir/[/b]%1.htm? [R]
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