Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Query] Trailing slash doesn't match routes. #23

Closed
BenJenkinson opened this issue Jan 13, 2013 · 7 comments
Closed

[Query] Trailing slash doesn't match routes. #23

BenJenkinson opened this issue Jan 13, 2013 · 7 comments

Comments

@BenJenkinson
Copy link

Just a simple one. I tried creating the following route:

Route::get('hello', function()
{
    return View::make('hello');
});

But this route is then only accessible at example.com/hello and not example.com/hello/, which throws a NotFoundHttpException. Obviously, creating the route for hello/ then means the route is no longer accessible at 'example.com/hello'. This is exactly what is supposed to happen, since the route didn't match, but it might not be what is expected to happen.

Type any URL on the Laravel 4 documentation site, absent-mindedly include a trailing slash, and you won't find the page.

This could be a feature when creating RESTful APIs (when a URI resource with a trailing slash is considered different from one without), but for standard navigation it could be a common query; especially since "Pretty URLs" commonly append a trailing slash for neatness.

To avoid this, users could simply define routes twice, in my case, for both hello and hello/.

Route::get('hello', function()
{
    return View::make('hello');
});
Route::get('hello/', function()
{
    return Redirect::to('hello')
});

However, I'm pretty sure doing this for every route for which a trailing slash could be a possibility would be quite annoying. On the other hand, you also wouldn't want to assume and use the .htaccess file to automatically add the slash automatically to all routes.

tl;dr Not sure if bug or feature, and merely a 'gotcha' for new users.

@softlabs-2
Copy link

I have been having the same issue when you create Resource Controller using Route::resource('test', 'TestController') and creating the controller using ''php artisan controller:make TestController".

Then when you browse to /test or /test/create or /test/1 it works fine but when you add a trailing slash like /test/ or test/create/ it throws NotFoundHttpException.

Just wondering if this will remain how it is or if it is a bug to be fixed?

@pauljohnston
Copy link

I'm also having the same issue, anything with a trailing slash doesn't route and just shows an error.

@franzliedke
Copy link
Contributor

Duplicate content has to be taken into account when handling this. The proper solution, if anything, would be to redirect to one of the two variants (but only if the other one of them is defined).

@taylorotwell
Copy link
Member

Just don't use trailing slashes.

@BenJenkinson
Copy link
Author

I don't think that's really a solution. Users may use trailing slashes. We may construct our URLs specifically forgoing them, but a user may still type a URL that includes one. We should be able to cope.

Traditionally, the segments of a URL are emulating the folder structure. Where although /example/docs/routing may be a page on the documentation, /example/docs/ is considered the main index of the documentation and it would be reasonable to expect a page to exist there. The URL structure /example/docs would indicate that docs is a page of the example section, when it is actually an index in its own right.

I have found some web services with APIs that insist upon one format over the other, or that use / to indicate whether the RESTful resource returns a single item or a list of items; but I cannot find any public-facing websites that follow the current behavior and do not either serve identical content, or redirect to the preferred URL-format (whether that be with, or without a /).

Even http://laravel.com currently functions in this way, laravel.com/docs/ produces the documentation as well as laravel.com/docs does.

It is of course preferable that a single page does not exist at two different addresses, but at the very least it should redirect from one to the other.

@tomicakorac
Copy link

It's possible to fix this using .htaccess, by rewriting or redirecting to url either with or without trailing slash. It's also possible to enable both variants. Although I agree this should be changed so that na url works with or without /$.

@ghost
Copy link

ghost commented Mar 29, 2013

Just came across this issue too. Any URL with a trailing slash now returns a NotFoundHttpException since updating to the latest version of L4 this morning.

I'm also of the opinion that Laravel should redirect to one or the other.

axelitus added a commit to axelitus/laravel-framework that referenced this issue Oct 29, 2019
# |<----   Preferably using up to 50 chars   --->|<------------------->|
# Example:
# Add feature for a user to like a post


# (Optional) Explain why this change is being made
# |<----   Try To Limit Each Line to a Maximum Of 72 Characters   ---->|


# (Optional) Provide links or keys to any relevant tickets, articles or other resources
# Example: Github issue laravel#23


# (Optional) List all co-authors, so version control systems can connect teams.
# Example: Co-authored-by: Name <name@example.com>


# (Optional) Tags suitable for searching, such as hashtags, keywords, etc.
# Example: Tags: authentication, login, security


# --- COMMIT END ---
# === Remember to ===
#   * Capitalize the subject line
#   * Use the imperative mood in the subject line
#     Imperative verbs usage:
#       - Add = Create a capability e.g. feature, test, dependency.
#       - Drop = Delete a capability e.g. feature, test, dependency.
#       - Fix = Fix an issue e.g. bug, typo, accident, misstatement.
#       - Bump = Increase the version of something e.g. a dependency.
#       - Make = Change the build process, or tools, or infrastructure.
#       - Start = Begin doing something; e.g. enable a toggle, feature flag, etc.
#       - Stop = End doing something; e.g. disable a toggle, feature flag, etc.
#       - Optimize = A change that MUST be just about performance, e.g. speed up code.
#       - Document = A change that MUST be only in the documentation, e.g. help files.
#       - Refactor = A change that MUST be just refactoring.
#       - Reformat = A change that MUST be just format, e.g. indent line, trim space, etc.
#       - Rephrase = A change that MUST be just textual, e.g. edit a comment, doc, etc.
#   * Do not end the subject line with a period
#   * Separate subject from body with a blank line
#   * Use the body to explain what and why vs. how
#   * Can use multiple lines with "-" or "*" for bullet points in body
# --------------------
# === Use semantic versioning ===
#   - Add, Start: Increment SemVer MINOR version when there is a new capability.
#   - Drop, Stop: Increment SemVer MAJOR version when there is an incompatibility.
#   - Fix, Bump, Make, Optimize, Document: Increment SemVer PATCH version.
#   - Refactor, Reformat, Rearrange, Redraw, Reword: Increment SemVer PATCH version.
# --------------------
# === Usage ===
# Put the template wherever you want.
# Example:
#     ~/.gitmessage
#
# Configure git to use the template file by running:
#     git config --global commit.template ~/.gitmessage && git config --global commit.cleanup strip
#
# Or manually add the template file to the ~/.gitconfig file:
#     [commit]
#       template = ~/.gitmessage
#       cleanup = strip
# --------------------
axelitus added a commit to axelitus/laravel-framework that referenced this issue Oct 29, 2019
# |<----   Preferably using up to 50 chars   --->|<------------------->|
# Example:
# Add feature for a user to like a post


# (Optional) Explain why this change is being made
# |<----   Try To Limit Each Line to a Maximum Of 72 Characters   ---->|


# (Optional) Provide links or keys to any relevant tickets, articles or other resources
# Example: Github issue laravel#23


# (Optional) List all co-authors, so version control systems can connect teams.
# Example: Co-authored-by: Name <name@example.com>


# (Optional) Tags suitable for searching, such as hashtags, keywords, etc.
# Example: Tags: authentication, login, security


# --- COMMIT END ---
# === Remember to ===
#   * Capitalize the subject line
#   * Use the imperative mood in the subject line
#     Imperative verbs usage:
#       - Add = Create a capability e.g. feature, test, dependency.
#       - Drop = Delete a capability e.g. feature, test, dependency.
#       - Fix = Fix an issue e.g. bug, typo, accident, misstatement.
#       - Bump = Increase the version of something e.g. a dependency.
#       - Make = Change the build process, or tools, or infrastructure.
#       - Start = Begin doing something; e.g. enable a toggle, feature flag, etc.
#       - Stop = End doing something; e.g. disable a toggle, feature flag, etc.
#       - Optimize = A change that MUST be just about performance, e.g. speed up code.
#       - Document = A change that MUST be only in the documentation, e.g. help files.
#       - Refactor = A change that MUST be just refactoring.
#       - Reformat = A change that MUST be just format, e.g. indent line, trim space, etc.
#       - Rephrase = A change that MUST be just textual, e.g. edit a comment, doc, etc.
#   * Do not end the subject line with a period
#   * Separate subject from body with a blank line
#   * Use the body to explain what and why vs. how
#   * Can use multiple lines with "-" or "*" for bullet points in body
# --------------------
# === Use semantic versioning ===
#   - Add, Start: Increment SemVer MINOR version when there is a new capability.
#   - Drop, Stop: Increment SemVer MAJOR version when there is an incompatibility.
#   - Fix, Bump, Make, Optimize, Document: Increment SemVer PATCH version.
#   - Refactor, Reformat, Rearrange, Redraw, Reword: Increment SemVer PATCH version.
# --------------------
# === Usage ===
# Put the template wherever you want.
# Example:
#     ~/.gitmessage
#
# Configure git to use the template file by running:
#     git config --global commit.template ~/.gitmessage && git config --global commit.cleanup strip
#
# Or manually add the template file to the ~/.gitconfig file:
#     [commit]
#       template = ~/.gitmessage
#       cleanup = strip
# --------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants