Forum Moderators: phranque

Message Too Old, No Replies

adding trailing slash problem

         

Maxwell Evans

6:09 pm on Apr 10, 2011 (gmt 0)

10+ Year Member



Hello,

This is my first post here, i've been trying for quite some considerable time to sort this problem. I've read a lot of articles and naviagted many tutorials all to no avail. I saw on google, quite a lot of threads relating to a similar problem so thought i'd ask for some help here as people seem to know what there talking about.

I have a site that i'm developing and i wanted to automatically append the trailing slash, as my current rules allow for them to be either a "www.example.co.uk/page" or "www.example.co.uk/page/".

I thought that it looked tidier with the slash.
My page structure isn't very complex either. I'm only passing at most one variable. They pages look like either:

www.example.co.uk/page
or
www.example.co.uk/page/someone

This is the set of rules I have made to accomplish losing the '.php' extension, but i'm really struggling getting the rules to add the slash and display the page properly. I mostly find that i can add the trailing slash but this then gives me a 404 error or the variables arent passed sadly.


RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^page/?$ page.php
RewriteRule ^page/([A-Za-z\+\-]+)/?$ ./page.php?x=$1

Any help would be massively appreciated.

Thanks in advance

Max

g1smd

8:45 pm on Apr 10, 2011 (gmt 0)

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



The URL for a folder or for the index page of a folder ends with a trailing slash.

The URL for a page does not end with a trailing slash, and may or may not have an extension.

You need a 301 external redirect to remove the slash when the request does not match a folder, and then a separate internal rewrite to process extensionless URL requests. Both will use RewriteRule syntax.

Maxwell Evans

9:39 pm on Apr 10, 2011 (gmt 0)

10+ Year Member



Thanks for getting back to me.

I understand what you've said more so thank you. I have no idea how to implement that though. Would you mind helping me some more?

Am I able to leave my rules as they are and expand on them as you've said or do i need to modify or remove any of the current rules?

Also, can i just clarify that I have no folders in my site, I will only have www.example.co.uk/page.php + www.example.co.uk/page.php?get=something that i wanted to become www.example.co.uk/page/ + www.example.co.uk/page/something/ respectively. Even if i didnt use the trailing slash

Just wanted to confirm this as i'm really in unsure waters here and didnt want to lead anyone astray with my poor rules or definitions.

Thanks again for your help, i'd really like to get a definitive answer for this so I use all the logic and understanding on more projects. :)

[edited by: Maxwell_Evans at 9:54 pm (utc) on Apr 10, 2011]

g1smd

9:48 pm on Apr 10, 2011 (gmt 0)

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



You have no folders?

Where do you store images, stylesheets, and scripts?

Having those in folders can help organise your URL space.

Maxwell Evans

9:53 pm on Apr 10, 2011 (gmt 0)

10+ Year Member



Oh yes forgive me. I store my css and js in folders. I wasnt thinking of the big picture. I was just thinking of the pages themselves.

g1smd

10:29 pm on Apr 10, 2011 (gmt 0)

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



Pages in folders is an outdated idea, especially on sites with multi-faceted navigation. :-)

Your initial code is a good start, but there is more to do. In outline:

You need the external redirect to remove the slash for extensionless URLs which are not real folders inside the server. This redirect needs to set www for those URLs at the same time.

You need an external redirect so that if URLs with parameters are requested, user is redirected to the correct extensionless URL. This redirect prevents Duplicate Content issues and will set www at the same time for those URLs. This ruleset will also need a preceding RewriteCond testing THE_REQUEST so that only direct client requests are redirected.

You'll also need a non-www to www external redirect to prevent Duplicate Content issues.

You need the internal rewrite to fetch the content. This will be the last rule in the file.

There are several prior posts with very detailed code examples, and details of how it all works.

The redirects will use the [R=301,L] flags and the rewrite will use the [L] flag.

Maxwell Evans

10:47 pm on Apr 10, 2011 (gmt 0)

10+ Year Member



Thanks g1smd,

Appreciate the help. I'm hitting the hay now. But tomorrow i'll give this forum a good read to try and learn more and construct a set of rules that works. I shall no doubt be back soon,

Thanks so far

Maxwell Evans

5:50 pm on Apr 11, 2011 (gmt 0)

10+ Year Member



Can i just ask also, what you meant by this "Pages in folders is an outdated idea, especially on sites with multi-faceted navigation. :-)"

What do you suggest instead?

g1smd

6:04 pm on Apr 11, 2011 (gmt 0)

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



www.example.com/gadgets/thiswidget
www.example.com/widgets/thiswidget
www.example.com/doodads/thiswidget
www.example.com/saleitems/thiswidget

are all Duplicate URLs for the same content.

Use just www.example.com/thiswidget or www.example.com/products/thiswidget and link that page from every category index page the product belongs to.

Maxwell Evans

7:02 pm on Apr 11, 2011 (gmt 0)

10+ Year Member



aaahh! I think I understand what your saying. And i think that i've read this during my .htacces mod_rewrite initiation.

Basically run all pages through the index page? Like a modularised website then use rewrite mods to clean up the urls?

ps - just about to read the threads on adding the trailing slashes. Hopefully be able to come up with some rules ;-)

pps - thank you

g1smd

7:11 pm on Apr 11, 2011 (gmt 0)

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



Most of the threads on WebmasterWorld discussing adding trailing slashes say "don't".

URLs for pages should be without a trailing slash.

URLs are defined in links. Link to the exact URL you want users to see and use. Use an internal rewrite to connect incoming requests to the internal script where the content resides.

Maxwell Evans

7:40 pm on Apr 11, 2011 (gmt 0)

10+ Year Member



Oh really! I can see why now you mention it. I look at a lot of sites and they operate a trailing slash (probably wordpress). I'd be happy with either ^page or ^page/ so I could stop duplicate content whilst maintaining a tidy look. I've started with the trailing slash so i'll give that a go. Then I can always revamp it. As long as I work hard to get this bit right.

This is such a complex specific concept. I'm really pleased that I found this forum with people who really know there stuff.

Thanks

g1smd

7:45 pm on Apr 11, 2011 (gmt 0)

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



When you have a URL with an extension it will be for a stylesheet or an image or for a static HTML page (i.e. one not served by the WP script system).

When you have a URL with a trailing slash it will be for a folder.

When you have a URL with no extension and no trailing slash it will the the URL for a page (one served by the WP script system).

Sticking to these formats makes URL rewriting a whole lot easier, the patterns much simpler and the whole process much more efficient.

Maxwell Evans

9:41 pm on Apr 11, 2011 (gmt 0)

10+ Year Member



Thinking about what you've said i think it is a good idea to have no trailing slash. So i'm going to rethink my approach read some more stuff in the forum and try (mercifully) to sort this out. Appreciate all the help so far. If i return then I'll start a new post as this will be titled remove trailing slashes and ultimately be about removing them

Thanks again

:-)