How to 301 Redirect Dynamic URLs With Your .Htaccess File

  1. Great post! I’ve been trying to figure out how to redirect these individual dynamic URLs for a while now!!! Thank God for your post…helped out a bunch. Keep up the good work.

    • @SEOinHawaii thanks for the kind words! To answer your question, I think you’d just have to expand the expression.

      For example, it would have to be written as;

      RewriteCond %{QUERY_STRING} ^id=4&mscsid=49$

      You’d just have to include everything after the question mark. I can’t think of any other way to do more than one ID.

      Hope this helps!

  2. Hi,
    I´ve been trying to apply 301 redirect on my .htaccess, but It doesn´t seem to work.
    I need to reedirect:

    Heres the code in my .htaccess:

    RewriteCond %{QUERY_STRING} ^id=1$
    RewriteRule ^masajes-masajista-drenaje-linfatico-enfermeria-primeros-auxilios-tecnico-electrocardiograma-reflexologia.php$ [R=301,L]

    what am I doing wrong?

    Thanks in advance!

    • It looks like that should work, and clicking your link, it seems it’s redirecting fine. Did you change anything to make it work?

  3. HI there,

    Great help here and I could help a friend out with his redirect thanks to you!

    Now I have a problem:

    Must redirect from a former framebusting script where blogs has noted dynamic urls like:

    Above dynamic url shall then be redirected in .htaccess to new static url like below:

    Have tried a lot of different redirects but it wont work. Has the page at

    Can someone please help me out here?

    Think I can pay some for this help too, if it works fine!

    Best Regards
    Eric A.

    • Hi Eric,

      The redirect examples above should work for what you’re trying to do. It should redirect a dynamic url to a static url.

      Sorry I couldn’t be of more help. Please leave another comment if you figure it out.

  4. I’ve been trying to figure this out for a couple of hours today! I am sooooo glad i found your post and instructions. They saved my ass! Much appreciation from South Beach!

  5. It´s not easy directing dynamic pages.
    I´ve searched all over for the solution that worked for me!
    You made it happen 🙂

    Thank you!

    • Hi Greg, Without fooling around with the htaccess file myself, it’s hard for me to give advice on those two examples. These are much more advanced redirect questions, that’s for sure. I can’t answer them myself, but I would definitely recommend you post your question at . I’m sure jdMorgan will be able to figure it out.

  6. Hmm, I’ve been trying but nothing seems to work. Attempting to redirect the first of these to the second:

    The code I used is:

    RewriteEngine On
    Options FollowSymLinks
    RewriteCond %{QUERY_STRING} ^name=News&file=article&sid=2$
    RewriteRule ^modules.php$ [R=301,L]

    Not sure if this code that comes before for the page to do google-friendly rewrites is affecting it:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    Very frustrating.

  7. Hi extramsg, I’m not sure why it’s not working, but you should try removing RewriteEngine On from the dynamic redirect part. That only needs to be in the htaccess file once and needs to be before the redirects.

  8. Thanks so much. I moved my site from an old .cfm site to wordpress. There were/are hundreds of .cfm redirects.

  9. Hello,

    Site used to be a .htm and now it is a Wordpress site…….

    Need to redirect an /index.htm to the root of the Wordpress site……

    When I add a… redirect 301 /index.htm
    to the htaccess, a looping problem occurs and the site will not load. Any thoughts on how to do with htaccess?

    Thank you,

    • Hi Oliver,

      Yes, redirecting index.htm and similar pages doesn’t work the same way. You will need to add this to your htaccess:

      RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /.*index.htm HTTP/
      RewriteRule ^(.*)index.htm$$1 [R=301,L]

  10. Alex,

    Thanks for the reply. I found a WP plugin called – Redirection

    It manages 301 redirects and monitors 404 errors. This worked for the /index.htm redirection.

    Can I have your opinion on which method you believe is preferable?

    htacces or WP plugin?


  11. Redirection is a great one but I like to use the .htaccess mainly because I worry about my site being bogged down by plugins. I also prefer to know what’s necessary to add to the htaccess in case I’m working on a site that isn’t running on Wordpress.

  12. I believe your opinion is very sound advice!

    One more question, should the redirection code you provided go before or after the WP mod rewrite code in the htaccess?

    Thank you again for answering these questions.

    • Hi Oliver, It can go after the WP mod rewrite code. I don’t believe it matters too much actually, as long as it comes after “RewriteEngine On”

  13. Hi Samiullah,

    You will need a different type of rule to create your redirects if you don’t want to input one rule for each one.

    You will need to use regular expressions for this. This means that all of your URL’s will need to contain the same pattern. Or you can create a new rule for each pattern.

    It would probably look something like this:

    RedirectMatch 301 ^play.php?id=(.*).htm$$1.html

    The (.*) is a variable. The 1.html is what the rule will append to the redirect, so play.php?id=1753 will redirect to 1753.html. This one might work or you might have to play around with it a bit.

    In your example you had asked that it redirect to a “Game_Name.” If you absolutely must have this, the .htaccess file probably isn’t the place you will be setting this stuff up because the regex above needs a pattern and “Game_Name” is random. Instead you will have to set up some php code and a lookup table for your database.

    I’m not familiar with this so I can only lead you to some resources. Here’s some more info on htaccess code that could help:

    If you still can’t figure it out, I would recommend asking the forums at

    And if you decide to do one redirect at a time, you should place the most important redirects at the top of the htaccess so that they are accessed first, reducing slow down from the server trying to find the correct redirect.

  14. Hi Alex,

    Thanks for your immediate response…Great one…I have one more doubt…i want to redirect to home page if a user tries to access a page which shows some ugly page…so i want to redirect to home if any user uses this kind of urls..i have as many as 5000 urls for my site…

  15. Hi Itay,

    I think it’s because you don’t have a space after the question mark, so instead it’s appending [R=301,L] to the end of the URL.

  16. Its me again can you guide me how to get in hyperlink also that link. Like if i have < a href="index.php?option=content&link_id={dynamic ID}" rel="nofollow">Content
    It should be seen in status bar as: index.html/content/?id={dynamic ID}.
    Thank you hope you will guide me!!

  17. Hi Jay,

    For your first question, this should work:

    RewriteCond %{QUERY_STRING} ^option=content&link_id={dynamic ID)$
    RewriteRule ^index.php${dynamic ID)? [R=301,L]

    For your second question, I’ve never done something like that. Maybe this page can help you;

    You could always just set up redirect so that the clean URL redirects to the long URL instead too.

  18. Hy to all,

    Here my question:

    I have to rewrite some urls with path like:


    to my new homepage

    I used this:

    RewriteCond %{QUERY_STRING} ^Action=tlc$
    RewriteRule ^main/default.aspx$ [R=301,L]

    the problem is that the final url become:

    but I need only:

    Onother problem is: how I can get a simple rule for multiple page with same pattern “/main/defautl.aspx?Action=”

    Thanks Bye!

  19. Hi,
    My Old Urls :

    my new url there is not.
    I want all oyna.php urls redirect

    My Code is
    RewriteRule ^oyna.php?oyun_id=(.*)$ [L,R=301]

    But not working this code.

  20. thanks for the great article.

    I have made a redirection following your guideline and it works,
    BUT it adds ? in the end of the new url.

    Here is my code:

    RewriteCond %{QUERY_STRING} ^main_page=product_info&products_id=301$
    RewriteRule ^index.php$
    /shop/index.php?route=product/product&product_id=301? [R=301,L]

    I think it’s because I still have a ? in my new url and it doesn’t understand what the second one is for…

    Thanks for your help!

  21. Hi Alex
    Pls help
    I’m trying redirect this url to

    Ive tried:

    1)RewriteRule ^new.html ?id=11 [NC]

    2)RewriteCond %{QUERY_STRING} ^id=11$
    RewriteRule ^(.*)$ mynew.html.html[R=permanent,L]

    1) allows me to acces the same web page via both urls. I was hoping to be redirected to when I type in the browser.
    2) Send me a 404 error
    Thanks in advance


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About The Author

A photograph of Alex Juel at a lake beach.

Alex Juel

Alex’s specialty at Inflow is to link it up, wrap it up, get it out there and get results. He has experience with clients large and small in a wide variety of industries.

View Author’s Profile

Request a Proposal

Send this to a friend