Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite slash to no slash issue

         

flanok

6:18 pm on Sep 28, 2007 (gmt 0)

10+ Year Member



Hi,

My site is a PHP dadabase driven site where all files should end without the /

i.e. example.co.uk/insurance/cheap

However example.co.uk/insurance/cheap/ also works and so duplicate content could be an issue. I would like the trailing slash to be diverted to the non trailing slash, or if it is better, the other way round.

I am well aware this question has been asked many times and I have scanned through many old posts.

I understand what the code will do, but I am not a coder and just get confused when it is all explained to me.

My solution has been to read the posts and copy and paste as many mod rewrite code into my .htaccess file until I find one that works and have done this for non www to www and index to / issues which work ok.

But even looking at past posts I cant find a version that works.

So i am posting my .htaccess (minus real url) to see if the existing version is affecting any more additions or if someone can give me another version of the rewrite that may work. can you help?

here is my current .htaccess. What can I add to send / to non /

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(example\.co(\.uk¦m)¦www\.example\.com) [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index.php$ http://www.example.co.uk/$1 [R=301,L]

# Is it not a file
RewriteCond %{REQUEST_FILENAME} !-f
# Is it not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ manager.php?url=$1 [L]

What can i add to this?

Thanks

Mark

[edited by: jdMorgan at 9:25 pm (utc) on Sep. 30, 2007]
[edit reason] example.com, example.co.uk [/edit]

g1smd

9:22 pm on Sep 29, 2007 (gmt 0)

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



You will want to test for URLs that end in "/" (and are from any domain) and then redirect the path ^(.*)/$ over to $1 at the correct domain.

However, I always use the URL that does have the trailing "/" included, as the one to be indexed.

flanok

11:20 pm on Sep 29, 2007 (gmt 0)

10+ Year Member



Thanks for the reply.

So are you saying I have to do this per every URL link to my site that ends in a "/"?

I was hoping for some code for my .htaccess so no matter who links to me and if they link using the "/" it will automatically redirect.

As i said in the previous post, I dont understand the code, just want some help, so i can add whetever will do the job

Mark

g1smd

12:01 am on Sep 30, 2007 (gmt 0)

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



Yes it is just a few lines of code, not one line for each one.

One problem in your existing code. The "index" redirect needs to be first in order to avoid a redirection chain.

The general catch-all for all other non-www URLs should be the very last rule.

flanok

2:06 pm on Sep 30, 2007 (gmt 0)

10+ Year Member



Hi again
Thanks for the reply, but I dont understand the it.

I understand what you are saying but just dont have the knowledge to make the complete 2 lines up.

I just want and example of the the 2 lines to try.

The examples i have tried from previous posts have not worked.

I have now swapped the index part over, so it now first.

Mark

g1smd

5:14 pm on Sep 30, 2007 (gmt 0)

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



Let's see an example of the closest you got to getting it to work.

Patterns that you will need in your code were given above.

flanok

7:22 pm on Sep 30, 2007 (gmt 0)

10+ Year Member



Here are 2 versions I have tried and neither work

Both versions do nothing, as if it wasn't there

RewriteCond %{REQUEST_URI}!^[^.]*/$
RewriteRule (.+) /$1/ [R=301,L]

and

RewriteRule %{REQUEST_FILENAME} index.php [L,NC]
RewriteRule ^(([^/]+/)*([^./]+))/?$ index.php?x=$1 [L]

The content is databased driven, not real pages ans all finsh non slash

Mark

jdMorgan

9:23 pm on Sep 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The reason this thread has gone on so long is that the purpose of this forum, as described in its Charter [webmasterworld.com], is to discuss Apache server, answer specific questions, and assist Webmasters in getting their own code working -- not to provide a free code-writing service, as this would simply be unsustainable.

Now that we have some example code to work with, here is how I would suggest modifying it, along with line-by-line comments that, in conjunction with the documents cited in our Charter, may aid in understanding it.


# If requested URI contains no periods or slashes in final path-part
RewriteCond %{REQUEST_URI} !^/([^/]+/)*[^./]+$
# Externally redirect to append a slash
RewriteRule (.+) http://www.example.co.uk/$1/ [R=301,L]

Jim

g1smd

9:58 pm on Sep 30, 2007 (gmt 0)

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



Heh. Wrong way round; wants to go from / to no-/ although I don't like that idea much.

flanok

10:55 pm on Sep 30, 2007 (gmt 0)

10+ Year Member



Hi Guys,

Thanks for the reply.
I am sorry I have broken some sort of charter.

The bottom line is I am pretty good at SEO but can never get my head around any coding, thats a fact.

I have tried this variation of code but does not work as it leaves bare pages with just the words, it takes away the design of the site.

Time to give it up here.

Thanks again

mark

g1smd

11:03 pm on Sep 30, 2007 (gmt 0)

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



Heh, we're here to help you learn how to do this, not just trot out code.

My previous post was pointing out that jd slightly misread the question.

jdMorgan

12:09 am on Oct 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yup, got it backwards...

# If requested URL does not resolve to an existing directory
RewriteCond %{REQUEST_FILENAME} !-d
# Externally redirect to remove trailing slash
RewriteRule ^(.+)/$ http://www.example.co.uk/$1 [R=301,L]

Jim