Url rewriting

Hi,

I am new to seo and I am trying to learn url-rewrite on htaccess/Apachae. Here is what I am doing:

  1. RewriteEngine On
  2. RewriteRule ^page1.htm$ page2.htm //working
  3. Redirect 301 page1.html page2.html //not working
  4. RewriteRule ^http://localhost/sample/prog.pl?q=param:A;pageoffset=10$ http://localhost/sample/prog.pl?q=param:B;pageoffset=20 //not working
  5. RewriteRule ^http\:\/\/localhost\/sample\/prog\.pl\?q\=param\:A\;pageoffset\=10$ http://localhost/sample/prog.pl?q=param:B;pageoffset=20 //not working
  6. RewriteRule ^prog\.pl\?q\=param\:A\;pageoffset\=10$ prog.pl?q=param:B;pageoffset=20 //not working

After this, when I open page ‘http://localhost/sample/prog.pl?q=param:A;pageoffset=10’ in the browser, I get the content of this same page and not the page that is intended/directed (i.e. http://localhost/sample/prog.pl?q=param:B;pageoffset=20). Actually in 4-6, I am trying to attempt the same thing but none of this works.

Any suggestions?

Thanks for this great forum!

This line is mostly right. The period is special in regular expressions. It means match any character. If you want to match a literal period, then you’ll have to escape it with a backslash.

RewriteRule ^page1\.htm$ page2.htm

Redirect directives have a different syntax than rewrite rules. Rewrite rules use regular expressions and typically match against a relative path, relative to the directory where its .htaccess file lives. But Redirect does not use regular expressions, and it matches against the full path string, including a slash at the beginning.

Redirect 301 /page1.html /page2.html

Rewrite rules match against just the path. No schemes. No host names. And no query strings. If you want to match against any of those, you’ll have to do so within a rewrite condition, because a rewrite condition can choose what value it matches against, which means you can apply a test to any of the server variables.

Rewrite conditions have the option of matching using a regular expression, or using plain string equality. They can also use flags, such as [NC], which stands for No Case, and it makes the match case-insensitive.

RewriteCond %{REQUEST_SCHEME} =http
RewriteCond %{HTTP_HOST} =localhost [NC]
RewriteCond %{QUERY_STRING} ^q=param:A;pageoffset=10$
RewriteRule ^sample/prog\\.pl$ sample/prog.pl?q=param:B;pageoffset=20

If you want to learn more about rewrite rules and other Apache directives, there’s no better place than the official documentation.

saibl,

First, WELCOME to SitePoint!

From your six examples, you don’t have a clear understanding of mod_rewrite (or mod_alias for the Redirect). I created a tutorial (with many examples) and it’s helped many SitePoint members over many years (it’s linked in my signature).

  1. Okay

  2. While you should “escape” the dot character in the regex, the dot you have there will match any character including the dot character so that’s the reason why that works.

  3. Redirect is a mod_alias directive which requires that the redirection be absolute (internal or external). Add / (OR add http://example.com/).
    before page2.html.

      1. These all are incorrect because the RewriteRule can only examine the {REQUEST_URI} variable (you’ve specified that AND both the {HTTP_HOST} in #4 and #5 and {QUERY_STRING} in all three. You need to make these matches in RewriteCond statements before the RewriteRule.

Don’t worry, all that is explained in the tutorial AND examples are given and explained following the tutorial.

Regards,

DK

That’s very helpful indeed. Thanks a lot Jeff, DK!