by lisag
5 replies
I always struggle with this. Could someone save me an hour on Google and help me do this?

I want to redirect:

sitename.com/article-nn (where nn is any number of any length preceded by a dash.)

to:

sitename.com/articles/articles.php?aid=nn (where nn is the same number as above.

Example:

sitename.com/article-1040

redirects to:

sitename.com/articles/article.php?aid=1040

Thanks!
#apache #rewrite
  • Profile picture of the author iampick
    ----------------- .htaccess Begin below this line ---------------
    RewriteEngine On
    RewriteBase /

    ## Change RewriteBase to you path. Use / for root path

    #### Rewrite URL #######
    RewriteRule ^article-([0-9]+)$ article-$1/ [R]
    RewriteRule ^article-([0-9]+)/$ articles/articles.php?aid=$1 [L]

    ----------------- .htaccess End before this line ---------------
    PS. I manually wrote script without testing.
    {{ DiscussionBoard.errors[1638391].message }}
    • Profile picture of the author lisag
      Originally Posted by iampick View Post

      ----------------- .htaccess Begin below this line ---------------
      RewriteEngine On
      RewriteBase /

      ## Change RewriteBase to you path. Use / for root path

      #### Rewrite URL #######
      RewriteRule ^article-([0-9]+)$ article-$1/ [R]
      RewriteRule ^article-([0-9]+)/$ articles/articles.php?aid=$1 [L]

      ----------------- .htaccess End before this line ---------------
      PS. I manually wrote script without testing.
      Thank you.

      Does the -([0-9]+) allow for a string of numbers of any length?

      0001 to 9999999999999, for example?
      Signature

      -- Lisa G

      {{ DiscussionBoard.errors[1638502].message }}
      • Profile picture of the author saschakimmel
        Originally Posted by lisag View Post

        Does the -([0-9]+) allow for a string of numbers of any length?
        0001 to 9999999999999, for example?
        Yes, it does

        Single numbers would look like this:
        ([0-9])
        Signature

        ** Get my ViralListMachine software now for free and build your own list virally by giving away free stuff @ http://www.virallistmachinegiveaway.com **

        {{ DiscussionBoard.errors[1639138].message }}
  • Profile picture of the author iampick
    I'm not sure how much of the length limited.
    -([0-9]+) is allow only number 0, 1, 2, ..., 9
    {{ DiscussionBoard.errors[1638809].message }}
    • Profile picture of the author saschakimmel
      She's been talking about Google - but this just sends a 302 redirect:
      RewriteRule ^article-([0-9]+)$ article-$1/ [R]

      You should always use 301 redirect to redirect if it is a permanent change:
      RewriteRule ^article-([0-9]+)$ article-$1/ [R=301]

      You can find more information on that from Google here:
      301 redirects - Webmasters/Site owners Help
      Signature

      ** Get my ViralListMachine software now for free and build your own list virally by giving away free stuff @ http://www.virallistmachinegiveaway.com **

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

Trending Topics