SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: url rewriting

  1. #1
    SitePoint Member
    Join Date
    Mar 2014
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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!

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,258
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by saibl View Post
    2. RewriteRule ^page1.htm$ page2.htm //working
    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

    Quote Originally Posted by saibl View Post
    3. Redirect 301 page1.html page2.html //not working
    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

    Quote Originally Posted by saibl View Post
    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
    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.

    Code:
    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.
    "First make it work. Then make it better."

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,650
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    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.

    4. - 6. 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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  4. #4
    SitePoint Member
    Join Date
    Mar 2014
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's very helpful indeed. Thanks a lot Jeff, DK!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •