Welcome to WebmasterWorld Guest from 54.205.115.177

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect wrong version to right file version

htaccess redirect

   
7:10 am on Sep 8, 2007 (gmt 0)

5+ Year Member



I have a htaccess file that i use make SE friendly urls. Following are the 2 rules i have in the htaccess file.

RewriteRule file/mid/(.*)/ file.php?mid=$1
RewriteRule file/mid/(.*) file.php?mid=$1

The problem now is that due to some external links pointing to the wrong version of the page, i now have 2 versions of some files in the google index.

Example of what i mean :
http://www.example.com/a-file/mid/478 - wrong version
http://www.example.com/a-file/mid/478/ - right version

http://www.example.com/a-file/mid/599 - wrong version
http://www.example.com/a-file/mid/599/ - right version

My right version file is with the ending slash.

How do i write a rule to redirect permanently all the wrong version (without slash) to right version (with slash) keeping in mind the existing rule above. So that i can remove the second rule (RewriteRule file/mid/(.*) file.php?mid=$1) that caused the problem.

Please guide me.

Thanks

[edited by: jdMorgan at 4:12 pm (utc) on Sep. 8, 2007]
[edit reason] example.com [/edit]

4:13 pm on Sep 8, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The second rule didn't really cause the problem, but allowing the wrong URL to resolve instead of redirecting it is what caused the problem. And there is another potential problem waiting for you, since someone could possibly link directly to the script, again creating duplicate content. Let's fix all of them:

# Externally redirect /file/mid/<file> requests to add a trailing slash if it's missing
RewriteRule ^file/mid/([^/]+)$ http://www.example.com/$1/ [R=301,L]
#
# Externally redirect direct client requests for the /file.php script back to the 'friendly' URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /file\.php\?mid=([^&]+)
RewriteRule ^file\.php$ http://www.example.com/file/mid/%1/? [R=301,L]
#
# Internally rewrite /file/mid/<file>/ requests to the script /file.php?mid=<file>
RewriteRule ^file/mid/([^/]+)/$ /file.php?mid=$1 [L]

Jim
2:56 pm on Sep 9, 2007 (gmt 0)

5+ Year Member



Hi Jim

Thanks for the help.

First of all i tried using the follwing to see if it adds a trailing slash:
# Externally redirect /file/mid/<file> requests to add a trailing slash if it's missing
RewriteRule ^file/mid/([^/]+)$ http://www.example.com/$1/ [R=301,L]
Nothing happened, there was no slash been added.

Then i realised that you missed put putting file/mid after www.example.com. So i modified the rule to:
RewriteRule ^file/mid/([^/]+)$ http://www.example.com/file/mid/$1/ [R=301,L]
Still nothing happened.

Then i thought its on the same domain so let me do the following:
RewriteRule ^file/mid/([^/]+)$ ^file/mid/$1/ [R=301,L]
Again nothing happened

Then i did:
RewriteRule file/mid/([^/]+)$ ^file/mid/$1/ [R=301,L]
This time the slash is added but the final url looks like this:
http://www.example.com/home/username/public_html/example/%5efile/mid/478/
Here the full server path is added to the url: "home/username/public_html/example/%5e"

Finally i did:
RewriteRule file/mid/([^/]+)$ http://www.example.com/file/mid/$1/ [R=301,L]
The problem solved partially.
What happended is that the slash is added and the page is displayed full but a part of the url is totally removed. Here is what i mean:
Original URL with Slash : http://www.example.com/variable-file/mid/478
New URL after Slash added : http://www.example.com/file/mid/478
"Variable-" is removed. How do i get this in the url too?

Thanks

3:17 pm on Sep 9, 2007 (gmt 0)

5+ Year Member



Hi Jim
I managed to solve the issue.
Here is what i did :
RewriteRule (.*)-file/mid/([^/]+)$ http://www.example.com/$1-file/mid/$2/ [R=301,L]

Thanks a lot for the help.

4:10 pm on Sep 9, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Assuming you have only one hyphen in the "variable-file/" part, this would be more efficient:

RewriteRule ^([^-]+)-file/mid/([^/]+)$ http://www.example.com/$1-file/mid/$2/ [R=301,L]

If you might have more than one hyphen in that part of the URL-path, then use:

RewriteRule ^(([^-]+)-)+file/mid/([^/]+)$ http://www.example.com/$1-file/mid[b]/$3/[/b] [R=301,L]

Avoid the use of ".*" whenever possible; It is simple and easy to understand, but is inefficient and often leads to unexpected results, because it matches *anything* and it matches as many characters as possible. Use specific character patterns, or specific negative-match patterns (as used above).

Jim