Forum Moderators: phranque

Message Too Old, No Replies

Remove Trailing Slash

         

Gaurang

7:32 am on Jul 17, 2012 (gmt 0)

10+ Year Member



I have problem with removing trailing slash:

I have pages like:

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

which needs to redirect to example.com

my .htaccess code is:

RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^(.*)/$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.php !-d
RewriteRule ^(.+)/$ /$1

But still problem is not resolved...

Can anyone help me out to resolve this problem?

g1smd

7:16 pm on Jul 17, 2012 (gmt 0)

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



I see nothing in that code that matches the example URLs.

You'll need to add another rule for those redirects, but redirecting may not be the best thing to do.

Gaurang

8:53 am on Jul 18, 2012 (gmt 0)

10+ Year Member



So can you please suggest what is the best thing to resolve this problem...

lucy24

12:14 pm on Jul 18, 2012 (gmt 0)

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



You need to start by explaining what the problem is. You say "trailing slash" and then you list several URLs that don't have trailing slashes-- and that apparently need to have something entirely different done with them.

You also need to figure out where the trailing slashes are coming from. Are you getting flooded with bad links from sites that refuse to fix things at their end? Or, if the problem is internal: if you've got any kind of CMS rewriting, you can't just jump in and redirect.

The RewriteCond with the first of your two Rules seems to be completely superfluous, unless you've got subdomains you haven't mentioned.

Gaurang

6:53 am on Jul 25, 2012 (gmt 0)

10+ Year Member



I am sorry to confuse you. If you notice the URL I gave you will see training file name which appear randomly for example below should be actual URL:

example.com/abc.php
example.com/erd.php
example.com/abc.php

but search result puts trailing slash and any random file name like below:

example.com/erd.php/yte.php
example.com/abc.php/yte.php

please help me to resolve this problem..

lucy24

7:10 pm on Jul 25, 2012 (gmt 0)

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



Oh, ouch. Oh, ouch. I think you have at least two problems and possibly more.

When you say "search results" do you mean the page URL that's visible in search-results listings? Or do you mean that when you click on a search result, you end up on a page with extraneous stuff?

RewriteCond %{REQUEST_FILENAME}.php !-d

Unless I'm reading this upside-down and sideways, it means "If you add '.php' to the end of the requested filename, it is not an existing directory." Uhm.. Wouldn't this condition always succeed?

You need two things. One, find out where that extra "xyz.php" business is coming from and put a stop to it. Two, snip off the extra bit.

If you are lucky, the second part can be achieved by

RewriteRule ^([^.])\.php(.+) $1.php [R=301,L]

meaning simply "If there is any more stuff of any kind after the first occurrence of '.php', get rid of it." Query strings don't count as "more stuff" so you need not think about them. Unless you're also getting garbage in your queries.

Which is why I started out saying "at least two problems".

Gaurang

10:29 am on Jul 26, 2012 (gmt 0)

10+ Year Member



thanks for your reply..

I tried putting string you gave in my .htaccess but it did not work.

Let me clarify my problem in little more brief. When i search google with "site:www.example.com" It shows the results the way i shown in my previous post i don't know why, and the the extra file name it adds in actual URL are also from my site only.

I don't know how to overcome this issue but looks like some tweaks on .htaccess might help.

Look in to the matter and see if you could help see below my current .htaccess.

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^(.+)/$ [%{HTTP_HOST}...] [R=301,L]
RewriteCond %{REQUEST_FILENAME}.php !-d
#and it has a trailing slash then redirect to URL without slash
RewriteRule ^(.+)/$ /$1 [L,R=301]
RewriteRule ^([^.])\.php(.+) $1.php [R=301,L]

We have written strings to
1. NonWWW to WWW redirection.
2. We also get trailing slash on our urls but changing in htaccess solved it

now trying to solve issue we have been discussing.

g1smd

12:52 pm on Jul 26, 2012 (gmt 0)

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



There are many errors in those rules.

g1smd

12:52 pm on Jul 26, 2012 (gmt 0)

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



There are many errors in those rules.

lucy24

10:02 pm on Jul 26, 2012 (gmt 0)

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



And there is one typo in my post. Ouch, ouch, sorry, mea culpa. And I just got through telling someone else that the advantage to posting in the Forums, over asking privately, is that someone else will always come along and catch your typos.

RewriteRule ^([^.]+)\.php(.+) $1.php [R=301,L]

The original version left out an absolutely essential + sign.

g1smd

10:39 pm on Jul 26, 2012 (gmt 0)

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



Referrring to msg:4479019

Escape all literal periods in RegEx patterns.

Each redirect target should include protocol and hostname.

^([^.])\.php(.+) matches only a single character before .php here.

When redirecting to strip trailing slash, the originally requested hostname is unimportant.

The test for "-d" seems completely inappropriate.

Also essential, a leading slash before $1 unless you want to leave your server open to hackers.

You should have a canonical non-www/www redirect after all your other redirects.

Comment each ruleset and add a blank line after each RewriteRule for readability.

Gaurang

6:11 am on Jul 27, 2012 (gmt 0)

10+ Year Member



It's working properly now. Thanks for your great support.

g1smd

6:29 am on Jul 27, 2012 (gmt 0)

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



Let's see the new code...

lucy24

12:33 pm on Jul 27, 2012 (gmt 0)

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



^([^.])\.php(.+) matches only a single character before .php here.

:: cough-cough ::