Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Aug 2011
    Location
    NY
    Posts
    37
    Thanks
    3
    Thanked 2 Times in 2 Posts

    301 redirect to another directory with dynamic links

    Using a 301 redirect in the .htaccess file, I am trying to get this url:
    http://custompatches.net/blog/?p=26

    To redirect to this url:
    http://custompatches.net/blog/


    Due to it being in a sub-directory as well as it being a dynamic link seems to be throwing a wrench in there.

    What code do I use, and do I put it in my main .htaccess file or the one in the blog folder?
    Last edited by StadriWebmaster; 09-09-2011 at 06:21 PM. Reason: giving more info

  • #2
    New Coder
    Join Date
    Jul 2011
    Posts
    67
    Thanks
    0
    Thanked 13 Times in 13 Posts
    I don't understand the question, the two links should display the same page, whatever is the DirectoryIndex of the blog folder.

  • #3
    New Coder
    Join Date
    Aug 2011
    Location
    NY
    Posts
    37
    Thanks
    3
    Thanked 2 Times in 2 Posts
    The don't display the same page. The first link goes the a specific blog post and the second goes to the blog homepage.

    I guess this would be a more accurate way to word it:

    I am trying to get this url:
    http://custompatches.net/blog/?p=26

    To redirect to this url:
    http://custompatches.net/blog/index.php

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    So you want to rewrite the URL to not have the query string?
    OracleGuy

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    It already does. Providing a querystring on the end of a non-existent page automatically assumes index, and will try in the order of extensions provided by apache configurations (.php is typically second). You're two links are the same except the first implements a querystring while the second does not. Since .html itself does not handle a querystring, this presumes you are only using index.php (or another server side processing language).

    If you want to issue a specific 301, you can do so with a simple:
    Code:
    RewriteCond %{QUERY_STRING} ^p=26$
    RewriteRule ^/blog/.*$ http://custompatches.net/blog/index.php [L,R=301]
    That looks like it would work. I have no idea why you want to do so though since they are still the same script :/. Seems easier to me (and more generic overall) to simply handle this from the PHP side instead.

  • #6
    New Coder
    Join Date
    Aug 2011
    Location
    NY
    Posts
    37
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Fou-Lu View Post
    It already does. Providing a querystring on the end of a non-existent page automatically assumes index, and will try in the order of extensions provided by apache configurations (.php is typically second). You're two links are the same except the first implements a querystring while the second does not. Since .html itself does not handle a querystring, this presumes you are only using index.php (or another server side processing language).

    If you want to issue a specific 301, you can do so with a simple:
    Code:
    RewriteCond %{QUERY_STRING} ^p=26$
    RewriteRule ^/blog/.*$ http://custompatches.net/blog/index.php [L,R=301]
    That looks like it would work. I have no idea why you want to do so though since they are still the same script :/. Seems easier to me (and more generic overall) to simply handle this from the PHP side instead.
    I want to do it because http://custompatches.net/blog/?p=26 no longer exists. Thanks for the code. I will give it a try.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I see.
    Its not really moved then, its removed, so you should probably issue a 410 GONE instead. That should force any indexing to remove it from their search lists (I would assume so anyway :/).
    I myself would still do this with PHP and issue a simple error message of my own.

  • #8
    New Coder
    Join Date
    Aug 2011
    Location
    NY
    Posts
    37
    Thanks
    3
    Thanked 2 Times in 2 Posts
    @Fou-Lu

    hmm...I didn't work. I put it in the .htaccess of my blog. Should I have put in the the .htaccess of my main directory instead?

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by StadriWebmaster View Post
    @Fou-Lu

    hmm...I didn't work. I put it in the .htaccess of my blog. Should I have put in the the .htaccess of my main directory instead?
    It shouldn't matter. Did you ensure that the rewrite_engine is on?

  • #10
    New Coder
    Join Date
    Aug 2011
    Location
    NY
    Posts
    37
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Fou-Lu View Post
    It shouldn't matter. Did you ensure that the rewrite_engine is on?
    Rewrite engine is on.

    This is the contents of my .htaccess file:


    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /blog/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /blog/index.php [L]
    </IfModule>

    # END WordPress


    RewriteCond %{/blog/?p=26} ref=custompatches.net
    RewriteRule ^(.*).html$ http://custompatches.net/blog/ [R=301,L]

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    First, I don't know what you're trying to do with this:
    Code:
    RewriteCond %{/blog/?p=26} ref=custompatches.net
    RewriteRule ^(.*).html$ http://custompatches.net/blog/ [R=301,L]
    That's not valid. There is no variable called /blog/?p=26. See the apache documentation here for rewrite rules and conditions: http://httpd.apache.org/docs/2.0/mod...ml#rewritecond

    But now I'm still confused. Has this entire site moved, or just a part of it or what?


  •  

    Posting Permissions

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