The problem:
Create a page display with a path like /articles (without using any Contextual filters or other fancy stuff). When you visit /articles/why-we-love-to-torture-kittens you get HTTP status 200 and the same content is delivered as if you'd visit /articles.
The aim:
Return HTTP status 404 if someone visits /articles/why-we-love-to-torture-kittens.
How to accomplish?
For Drupal 6 there is the module Views 404 (which might work for Drupal 7, too). But I wonder: is there another (maybe better?) way?
… with the help of a Contextual filter?
I added the following contextual filter (and it seems to work):
Contextual filter: 'Content: Type' with the following settings:
'When the filter value is NOT in the URL' → 'Display all results for the specified field'
'When the filter value IS in the URL or a default is provided' → 'Specify validation criteria'
'Validator' → 'Content'
'Content types' → (the article type)
'Filter value format' → 'Node ID'
'Action to take if filter value does not validate' → 'Show "Page not found"'
1) Does this have any drawbacks I don't think of atm?
2) And is there a better filter than 'Content: Type' (which I chose kind of random … because the filter doesn't really have any job, I guess … it should only react on additional URL arguments).
Comments
Comment #1
no2e commentedOh … using this contextual filter: whenever someone visits a non-existent page path, there is a PHP warning in the 'Recent log messages':
Same problem has hawkeye217 (#1414068: Fix PHP warnings when a View path 404/3's).
Comment #2
merlinofchaos commentedThis is the normal behavior that Drupal uses.
Here's an example:
Visit node/1
Now visit node/1/alskdjflk
Drupal does this all over the place. I'm not sure why people always think this is a bug in Views as though Views does something different than Drupal itself.
If you don't want this behavior, add the Global: NULL argument to Views and use the setting to validate that the argument is empty.
Comment #3
merlinofchaos commentedI'd never seen the Views 404 module before. Sigh. Spreading misinformation is awesome.
Comment #4
no2e commentedThanks for your suggestion!
It works when I use the Validator "PHP Code" without anything in "PHP validate code" (so the PHP text area is totally empty).
Is this the right validator to use or should/could I use a different one? (feels a little bit wrong because I always read that you shouldn't use custom PHP ;-))
I was aware that this is not considered a bug, hence my question to prevent (and not to "fix") it.
I think a reason why people might "blame" Views could be this:
Most sites will use Path/Pathauto for nodes. So
/node/42becomes/about. Now, you can append anything at/node/42and it will always be 200. But you can't append something at/about→ 404 (except you explicit created a path for it). So the user has full control over node paths. Now, the Views page path looks/feels more like a 'Path', not like a system path. And so the Drupal behavior "breaks": "all my readable paths for nodes work like they should, but the readable path created by Views is accepting any appendix".Comment #5
dawehnerYou seem to have a problem with the fundamental concepts of drupal:
internal paths and aliases. About is an alias, just a simple text, so appending changes the alias, that's it.
For interal paths you can have arguments which might change something, you know things like this got discussed so often. With global redirect i'm pretty sure you could this with it.
Just for example you can also user user/1/foo/bar oh sure that's duplicate content as well.
Comment #6
no2e commentedI understand the difference, that's why I wrote "the Views page path looks/feels more like a 'Path', not like a system path" (assuming that "system" and "internal" are interchangeable) – and that was a possible explanation (and not my point of view) for a reason why some people might think that Views has a bug here.
What exactly do you mean? I have Global Redirect installed, but can't find an option that might help for this case.
{{arg, why is
<hr>not allowed on d.o?}}Maybe another solution could be to use a cryptic Views page path (like
/views/1☺) and add a readable path for this View manually at/admin/config/search/path/add… need to try that.Comment #8
osopolarI came across this as on a clients website somehow google started to index these type of sites (as there is a pager and google thinks that these are valid urls). Finally this would result in duplicated content which is bad for ranking.
EDIT: The Global:NULL contextual filter helps, there is an option under more I overlooked before: "Fail basic validation if any argument is given (By checking this field, you can use this to make sure views with more arguments than necessary fail validation.)"