...

View Full Version : 301 redirect to another directory with dynamic links



StadriWebmaster
09-09-2011, 02:38 PM
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?

Cags
09-09-2011, 06:32 PM
I don't understand the question, the two links should display the same page, whatever is the DirectoryIndex of the blog folder.

StadriWebmaster
09-09-2011, 07:11 PM
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

oracleguy
09-09-2011, 09:22 PM
So you want to rewrite the URL to not have the query string?

Fou-Lu
09-09-2011, 09:28 PM
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:


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.

StadriWebmaster
09-09-2011, 09:54 PM
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:


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.

Fou-Lu
09-09-2011, 09:56 PM
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.

StadriWebmaster
09-09-2011, 09:56 PM
@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?

Fou-Lu
09-09-2011, 09:59 PM
@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?

StadriWebmaster
09-12-2011, 02:36 PM
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]

Fou-Lu
09-15-2011, 08:08 PM
First, I don't know what you're trying to do with this:


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/mod_rewrite.html#rewritecond

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum