Forum Moderators: phranque

Message Too Old, No Replies

using htaccess to remove trailing characters

I'd like to use htaccess to remove trailing characters

         

Ammalgam

3:01 am on Jul 4, 2012 (gmt 0)

10+ Year Member



Frustrating because it seems like it should be simple.

Client had a data snafu and a lot of merged pages in Wordpress.

domain.com/king-2
domain.com/king

are the same page.

Client wants to redirect all the -2 pages to the non -2 pages

So:

domain.com/king-2 -> domain.com/king

How could i use .htaccess to set up a rule to do this for all -2 pages?

We have deleted the -2 pages but obviously they will now give 404 errors for Google, hence the urgent need for the 301 redirect.

Thanks...

g1smd

6:56 am on Jul 4, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What have you tried so far? This needs a one-line RewriteRule to fix things up.

Ammalgam

7:03 am on Jul 4, 2012 (gmt 0)

10+ Year Member



RewriteRule ^(.*)-2$ [domain.com...] [L,R=301]

It didnt work for me...

g1smd

7:07 am on Jul 4, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It should have worked. Rule order was most likely the problem. This should go before all other redirects.

The rule can also be tidied by replacing (.*) with a more specific pattern.

Use example.com in this forum.

Ammalgam

7:11 am on Jul 4, 2012 (gmt 0)

10+ Year Member



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

This was the very first line of .htaccess and still didn't work.

Ammalgam

7:13 am on Jul 4, 2012 (gmt 0)

10+ Year Member



So

http://www.example.com/2011/02/22/microfgfg-strategy-for-fgfg-windows-on-tablets-is-criticized-2/

is not redirecting to

http://www.example.com/2011/02/22/microfgfg-strategy-for-fgfg-windows-on-tablets-is-criticized

Ammalgam

7:16 am on Jul 4, 2012 (gmt 0)

10+ Year Member



aaah fixed

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

The slash after -2 was missing

THANKS!

lucy24

8:02 am on Jul 4, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



... and all g1 did was sit back quietly while you figured it out yourself. Wasn't that satisfying? :)

Ammalgam

8:11 am on Jul 4, 2012 (gmt 0)

10+ Year Member



In a bizarre way it was...

That's terrifying...

:-)

g1smd

7:39 pm on Jul 4, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If some requests have a slash and others do not, use
-2/?$
or similar.

Using
(.*)
is bad. See if you can replace it with something more efficient.

Ammalgam

7:52 pm on Jul 4, 2012 (gmt 0)

10+ Year Member



They all have the slash so it works as is.

This forum kicks a$$

Gaurang

10:10 am on Jul 9, 2012 (gmt 0)

10+ Year Member



I have problem with removing trailing slash:

I have pages like:

mydomain.com/abc.php/ which i have resolved with following .htaccess code:

Options +FollowSymLinks
RewriteEngine on

# Remove trailing slashes.
RewriteCond %{HTTP_HOST} ^(www.)?mydomain\.com$ [NC]
RewriteRule ^(.*)/$ [mydomain.com...] [R=301,L]

but there are also some pages such as:

mydomain.com/abc.php/xyz.php
mydomain.com/erd.php/yte.php
mydomain.com/abc.php/yte.php

which needs to redirect to mydomain.com

Can anyone help me out to resolve this problem?

g1smd

9:07 pm on Jul 9, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You don't need the RewriteCond with that rule.

You should test the rule by requesting a real folder on your server (perhaps /images/ or similar). You'll likely find an infinite redirect loop. The rule pattern should likely include \.php to ensure it does not match requests for real folders.

In your additional new rules for the other URLs you mentioned, the pattern will change from (.*) to instead match the exact requested path.

Use example.com in this forum.

Gaurang

6:15 am on Jul 10, 2012 (gmt 0)

10+ Year Member



Hi g1smd,

Can you please explain in detail that what kind of changes require to modify my .htaccess.

My website has all static pages.

Which code should i place here:

# Remove trailing slashes.
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^(.*)/$ http://www.example.com/$1 [R=301,L]