Changing a script - permanent redirection question

by Toon-c
9 replies
Hi ..

I'm about to change the script which is running one of my oldest websites ..
I need to make a permanent redirection for about 300 pages (articles) ..

The old URLs are like
www.site.com/example-1/article-title-here.htm

with the new script installed , I would like all the URLs in that format to be redirected to
www.site.com/example-2/article-title-here

How can I do that ???

Will be thankful
#changing #permanent #question #redirection #script
  • Put the following into your website's root .htaccess file:

    Code:
    RewriteEngine on
    RewriteBase /
    RewriteRule ^example-1/([0-9A-Za-z\-]+).htm$ example-2/$1 [R=301,L,QSA]
    {{ DiscussionBoard.errors[2701352].message }}
    • Profile picture of the author SteveJohnson
      Originally Posted by Chris Landrum View Post

      Put the following into your website's root .htaccess file:

      Code:
      RewriteEngine on
      RewriteBase /
      RewriteRule ^example-1/([0-9A-Za-z-]+).htm$ example-2/$1 [R=301,L,QSA]
      You left a number of allowed characters out of your regex. It's probably better to let everything through since the rewrite's job is not to filter URIs, but to rewrite/redirect:
      Code:
      RewriteEngine on
      RewriteBase /
      RewriteRule ^example-1/(.+).htm$ example-2/$1 [R=301,L,QSA]
      Signature

      The 2nd Amendment, 1789 - The Original Homeland Security.

      Gun control means never having to say, "I missed you."

      {{ DiscussionBoard.errors[2830107].message }}
      • Originally Posted by SteveJohnson View Post

        You left a number of allowed characters out of your regex. It's probably better to let everything through since the rewrite's job is not to filter URIs, but to rewrite/redirect:
        Yes, I did leave a number of allowed characters out of the regex because the OP didn't require it (based on the provided example). Wildcards can be dangerous. While it may not make a difference to allow additional characters through on this example, security needs to be an important consideration for every component of a web site -- including an .htaccess file.
        {{ DiscussionBoard.errors[2832005].message }}
        • Profile picture of the author phpbbxpert
          Originally Posted by Chris Landrum View Post

          Yes, I did leave a number of allowed characters out of the regex because the OP didn't require it (based on the provided example). Wildcards can be dangerous. While it may not make a difference to allow additional characters through on this example, security needs to be an important consideration for every component of a web site -- including an .htaccess file.
          It definitely limits injections of all sorts
          {{ DiscussionBoard.errors[2832167].message }}
          • Profile picture of the author SteveJohnson
            Originally Posted by Chris Landrum View Post

            Yes, I did leave a number of allowed characters out of the regex because the OP didn't require it (based on the provided example). Wildcards can be dangerous. While it may not make a difference to allow additional characters through on this example, security needs to be an important consideration for every component of a web site -- including an .htaccess file.
            I don't mean to start a war here - but there was only one example given that may or may not be totally representative of the site's existing URI structure.

            If you arbitrarily apply your idea of "security", a redirect failure is possible/probable and the client will have to seek further assistance. Not good customer service.

            It is not the job of a rewrite rule to filter URL requests - and there is no danger in using wildcards because every printable US-ASCII character is allowed in a URL.

            Incidentally, you don't have to use the QSA flag as you're not adding to any existing query string.

            Originally Posted by phpbbxpert View Post

            It definitely limits injections of all sorts
            It really doesn't, as injections are done in the query string which isn't affected by this rewrite.
            Signature

            The 2nd Amendment, 1789 - The Original Homeland Security.

            Gun control means never having to say, "I missed you."

            {{ DiscussionBoard.errors[2832532].message }}
            • Originally Posted by SteveJohnson View Post

              I don't mean to start a war here - but there was only one example given that may or may not be totally representative of the site's existing URI structure.

              If you arbitrarily apply your idea of "security", a redirect failure is possible/probable and the client will have to seek further assistance. Not good customer service.

              It is not the job of a rewrite rule to filter URL requests - and there is no danger in using wildcards because every printable US-ASCII character is allowed in a URL.

              Incidentally, you don't have to use the QSA flag as you're not adding to any existing query string.

              It really doesn't, as injections are done in the query string which isn't affected by this rewrite.
              Why would there be a war? That would only be true if I needed to stroke a self-deluded ego. The one example, provided by the OP, used alphanumeric characters and a hyphen. So, it was assumed the same structure would account for the rest of the site. However, you are correct. That was presumptuous and a wildcard is the best decision for this example. Does this mean my code was incorrect? Not at all. But the wildcard protects against the possibility of a redirect failure (and there is no probable security risk in this example), so it is the best usage.
              {{ DiscussionBoard.errors[2835176].message }}
              • Profile picture of the author SteveJohnson
                Originally Posted by Chris Landrum View Post

                Why would there be a war? That would only be true if I needed to stroke a self-deluded ego.
                Chris, I've been around this gig a LONG time, and I've come to the conclusion that a self-deluded ego is standard equipment on most programmer types. It's refreshing to run across someone who isn't. Kudos to you

                Originally Posted by Chris Landrum View Post

                The one example, provided by the OP, used alphanumeric characters and a hyphen. So, it was assumed the same structure would account for the rest of the site. However, you are correct. That was presumptuous and a wildcard is the best decision for this example. Does this mean my code was incorrect? Not at all. But the wildcard protects against the possibility of a redirect failure (and there is no probable security risk in this example), so it is the best usage.
                You're right - given the example, yours wasn't incorrect at all. And I have to give you stars for even comprehending how mod_rewrite works. As shown on the Apache 2.2 docs front page for mod_rewrite:
                Despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo.
                Signature

                The 2nd Amendment, 1789 - The Original Homeland Security.

                Gun control means never having to say, "I missed you."

                {{ DiscussionBoard.errors[2837835].message }}
  • Profile picture of the author Toon-c
    Is this to rewrite or to redirect ?
    Signature
    HT Consulting S.A.R.L - Online marketing services - htconsulting.co
    Bluechain Solutions - Brand development services - bluechainsolutions.com
    {{ DiscussionBoard.errors[2829723].message }}
    • Profile picture of the author SteveJohnson
      Originally Posted by Toon-c View Post

      Is this to rewrite or to redirect ?
      It redirects. Notice the R=301 in the [flags] area. This tells Apache to tell the requesting browser that the page has moved permanently and to request the new address.
      Signature

      The 2nd Amendment, 1789 - The Original Homeland Security.

      Gun control means never having to say, "I missed you."

      {{ DiscussionBoard.errors[2830083].message }}

Trending Topics