Forum Moderators: phranque

Message Too Old, No Replies

htaccess to file with a query string

         

vero

8:29 pm on Feb 11, 2009 (gmt 0)

10+ Year Member



I need to redirect two currently static files to files that use a query string. Being a total noobie, I tried regular 301 redirect, found out I need to do a rewrite. But what I have tried thus far isn't working. This is the latest:

RewriteEngine on
RewriteRule ^/directory/file1.htm$ http://www.example.com/newdirectory/file1.htm?newdirectory/file3249861.htm [L,R=301]
RewriteRule ^/directory/file2.htm$ http://www.example.com/newdirectory/file2.htm?newdirectory/file3246731.htm [L,R=301]

Any help is so very much appreciated!

jdMorgan

9:37 pm on Feb 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your code is in .htaccess, then remove the leading slash from the two rewriterule patterns. In .htaccess, the URL-path matched by the rewriterule pattern is "localized" to the current .htaccess file's directory, and that path is removed from what rewriterule "sees". Since this .htaccess file's directory is "/", that path-part is removed, and should not be included in the rewriterule pattern.

Jim

vero

10:15 pm on Feb 11, 2009 (gmt 0)

10+ Year Member



Thank you! I removed the slashes, then realized I needed to remove the directory too, as I'd placed the htaccess into the directory. Works fine now.
Thanks for the explanation - it really helped.

jdMorgan

11:27 pm on Feb 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Now the question becomes, "Do you really want a redirect, and not an internal rewrite?"

If your intent is to replace the URL shown by search engines in their results listings, then use the redirect. However, if your intent is to simply serve the content from the filepath /newdirectory/file2.htm?newdirectory/file3246731.htm when the URL http://www.example.com/directory/file2.htm is requested by a client, then use the internal rewrite syntax instead.

Also, note that any literal periods in regex patterns must be escaped with a preceding backslash slash. If the periods are not escaped, they are interpreted as regex tokens meaning, "match any single character." So, your pattern should likely be "^file1\.htm$"

Jim

vero

1:24 am on Feb 12, 2009 (gmt 0)

10+ Year Member



My intent is to simply serve the content of the new file when the old file is requested. The URl being shown doesn't matter.

I'm trying to google info on different syntax for an internal re-write, other than what I've got and not having much luck. Do I just drop the R=301?

[edited by: vero at 2:02 am (utc) on Feb. 12, 2009]

jdMorgan

1:59 am on Feb 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The new path (the argument on the right end of the rule) is not a regular-expressions pattern, so it does not require escaping except in the rare cases where you want to inject a literal "$" or "%" character.

See the Apache mod_rewrite documentation on syntax differences between external redirect and internal rewrite. Also, there are dozens of examples posted here in this forum every day (and I always try to concisely comment my code)... :)

Jim

vero

2:25 am on Feb 12, 2009 (gmt 0)

10+ Year Member



Sorry - I got confused.
Thanks again.

g1smd

8:33 pm on Feb 12, 2009 (gmt 0)

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



*** The URL being shown doesn't matter. ***

It does matter, in that you need to pick one format to be the canonical format, and redirect any others to it. That prevents Duplicate Content indexing.