Here is a patch for path.inc. When drupal_lookup_path is called, if it tries to look up $path and fails and $path does not end in a '/' it then looks again for $path."/"

Not sure if this format will work for everyone, as I have generated it in Zend/Eclipse. Let me know.

Discussion Here:
http://drupal.org/node/160753

I can do a v7 patch if wanted. I don't see any downside to this patch, it simply gives more flexability in alias creation.

CommentFileSizeAuthor
#10 path_070908_160753.patch792 bytesKingMoore

Comments

KingMoore’s picture

This same patch should work for 7x-dev.

Without patch:
function drupal_lookup_path("source", $path)

Will look up $path

With patch:
function drupal_lookup_path("source", $path)

Will look up $path, then if not found and $path does not end in a '/' it looks again for $path."/"

Crell’s picture

Status: Needs review » Closed (works as designed)

I don't see how this is desirable. Entering a path alias with a trailing slash is a user error. We should not be supporting user errors; we should be educating users away from them.

KingMoore’s picture

Status: Closed (works as designed) » Needs review

Crell,

There is some discussion about it here:
http://drupal.org/node/160753

The situation where this patch is needed is not a user error, but rather when the user wants a node to get a url_alias of something like 'my_node/' because they want this node to appear like an index page of a directory.

I don't think you should label this a user error because it is not the way you would structure a site. I believe Drupal should offer the site builder the flexability to put pages at locations like 'my_node/' if they want. If you read the discussion at above node, you will see users have various reasons for wanting to do this.

Thanks.

Rowanw’s picture

Priority: Normal » Minor
Status: Needs review » Active

-1

It's pointless to have a trailing slash, the only reason you'd need it is if you're converting from an old system that used trailing slashes. Even then, it's not worth breaking Drupal (for lack of a better term) just because other systems are broken.

KingMoore, you say that you'd use a trailing slash to indicate that the current page is a directory, but to who? Google won't treat it any differently, the server won't treat it any differently, a typical web-user certainly won't know what it means. It's just a meaningless character that takes up valuable space and requires an extra keystroke.

I've built sites for clients who didn't know they could type their site's domain name in the address bar of their browser, and it's their own website! Do you think these people care about whether or not there's a trailing slash?

And going by your use case, when you add a new sub page, are you going to change the parent page's URL to add a trailing slash even if it didn't have one before? If so, you risk creating a dead link. If you decide not to add a trailing slash this time then you're being inconsistent.

If you want to be old-fashioned then use Pathauto to add .htm to every node, and your categories will look like directories because they won't have an extension. You see, it's not a trailing slash that indicates a 'directory', it's the lack of a file extension.

KingMoore’s picture

My point isn't that it is the best option in all situations, but that the site builder should have the flexibility to build his aliases as desired. Your arguments against structuring your site like this are good ones, however I don't see a real benefit doing it that way either. It is just that Drupal is currently restricted in this way, so there is a resistance to change, and would have to be an obvious benefit/reason for the change rather than just to provide more flexibility and options in configuring his/her site.

Upgrading an existing site structured in this way to Drupal is one huge example of where site builders want this flexibility. If you read the thread linked above, most of the people wanting this feature are people migrating sites from other systems, and don't want to risk deteriorating their rankings (even if temporarily) by switching to a system that can't preserve their old URLs w/out redirects. Multiple people looking to switch to Drupal have stated this as the main consideration for them NOT making the switch.

KingMoore’s picture

On more thing to note:

Currently in Drupal (at least 5.7) you can create new content, and set the path alias to something like 'test/'. It will allow you to save the content and Drupal will give no errors. A new row is inserted into the {url_alias} table with a dst of 'test/'. However, this page will never be able to be accessed and after saving, the user will be given a 'page not found'.

If we aren't going to allow the system to work with aliases like 'test/', then the saving of them being permitted is certainly a bug. However, I would rather see them working after the save, than not saving at all.

macgirvin’s picture

I hate to be a jigger here, but with the patch as described, you'd be doing two lookups whenever a path wasn't available - and potentially before a 404 (let's take the case where the path just plain isn't there).

Why not..... just trim a trailing slash if it exists, and do a single lookup? Is there any place in Drupal where the trailing slash is valid and useful? If not, let's get rid of it. And I'm not suggesting we punish the poor user for typing it. They get punished enough. Just drop the thing quietly and return the page.

KingMoore’s picture

That is what the system is currently doing. However if there is a path in the system that ends in a trailing slash (which the system does let you enter) then this path can never be found using current methods.

Crell’s picture

Title: Allow trailing '/' in aliases » Disallow trailing slashes in path aliases
Version: 5.7 » 7.x-dev
Category: feature » bug

Well, then this becomes a bug report against Drupal 7, with intent to backport. Should be easy to do in the appropriate validate hook.

KingMoore’s picture

Title: Disallow trailing slashes in path aliases » Allow drupal_lookup_path to retrieve saved paths with trailing slash
Version: 7.x-dev » 5.7
Category: bug » feature
Status: Active » Needs review
StatusFileSize
new792 bytes

His a new patch. It has been requested here: http://drupal.org/node/160753

It doesn't introduce an extra query. Note that if the path you are looking for is 'about/', drupal_lookup_path is actually sent just 'about' (the slash is stripped elsewhere and it would invove a much more complex patch to change that functionality).

The only downside of this patch is that you cannot have working paths in your system of both 'about' and 'about/'. Given that the original problem was that the Drupal allows saving of paths with a trailing slash, but then they could never be accessed, this is an improvement.

Here is an example of how it works, given the path 'about'.

drupal_lookup_path: 'about'
queries URL alias table for: 'about' or 'about/'

Return whatever comes back
If BOTH come back, only 'about' (without trailing slash) is returned.

j0hn-smith’s picture

subscribing

KingMoore’s picture

Category: feature » bug

I guess this should actually be a bug report as D5 allows you to save paths like 'about/', but drupal_get_path wont let you retrieve them.

drumm’s picture

Title: Allow drupal_lookup_path to retrieve saved paths with trailing slash » Disallow trailing slashes in path aliases
Version: 5.7 » 7.x-dev
Status: Needs review » Active

Hi, I am the Drupal 5.x maintainer, I do the final review for most patches going into Drupal 5.x. I appreciate your desire to get this patch into Drupal 5.x, but there have been some excellent reviews and you have simply pushed back. Please work with us and the issue will be resolved.

Path lookups happens a lot, one for each drupal-created link on the page, on every page request. Adding OR to the query does increase complexity, even a small performance loss might add up quickly. Benchmarks are necessary.

I am okay with adding a small amount of logic to the PHP code: removing trailing path slashes before lookup and ensuring paths are saved with slashes trimmed. In high-scalability situations, PHP load (more web servers) is much easier to handle than DB load (build out a database cluster).

Whenever multiple versions of Drupal need the same fix, we start with the highest and backport. The best code reviews happen on the more recent versions, and provide more opportunities for testing.

KingMoore’s picture

Hi Drumm,

Thanks for your thoughtful response. I realize that the ability to save paths with a trailing slash will probably never make it into drupal core. I refined and reposted this patch specifically because multiple users have asked me to repost it because they do want that functionality.

The underlying issue for them (and myself) is that we be able to preserve and lookup paths with trailing slashes. Hence the suggestions to simply patch drupal core to not allow us to save paths with trailing slashes, while a valid bug that should be fixed, does not solve our problem (we can't currently use paths with a trailing slash and we want/need to).

I do have a hard time believing that adding an OR to an SQL query would produce a measurable performance loss.

j0hn-smith’s picture

A trailing slash may be technically incorrect however my company has thousands of pages with a trailing slash that rank well in google, we will not risk losing this ranking for anything. We simply wouldn't use Drupal or any other system that didn't allow us to use our current URLs. Many other companies/sites must be in a similar position.

Fundamentally, you should be free to choose any URL you like.

This issue will restrict drupal usage, surely we should remove barriers wherever possible. I think this functionality should be in core with, if necessary, an option in the admin pages to choose whether to allow the additional slash.

Bilalx’s picture

sammys’s picture

subscribing

aaronnewton’s picture

Please see my comment here -

http://drupal.org/node/203632#comment-1509768

which explains with practical examples and authority links which it is always a good idea to leave in trailing slashes.

dave reid’s picture

Status: Active » Closed (duplicate)