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

Add a slash at the end of multilingual Home routes #57

Closed
monsieurbab opened this issue Sep 9, 2021 · 5 comments
Closed

Add a slash at the end of multilingual Home routes #57

monsieurbab opened this issue Sep 9, 2021 · 5 comments
Assignees
Labels
question Further information is requested

Comments

@monsieurbab
Copy link

Hello,
After configuring and installing this package, I realized that Home multilingual routes don't have a trailing slash.

Example :
-> Desired route: https://www.example.com/fr/
-> Route generated: https://www.example.com/fr

routes/web.php
Route::multilingual('/', [ShowHomepage::class])->name('homepage');

php artisan route:list
GET|HEAD | /fr | fr.homepage | Closure | web |
...

Is there a way to tell the package to leave the slash at the end only for the Home route?

Thank you for this package which is perfect for multilingual websites!

@chinleung
Copy link
Owner

Hey @monsieurbab,

Is there any reason why you would like a trailing slash only for the home routes?

@chinleung chinleung self-assigned this Sep 9, 2021
@monsieurbab
Copy link
Author

monsieurbab commented Sep 9, 2021

Thank you for the quick reply!

There is no particular technical reason. This is a "URL style" that we use on our current website and we would like to keep it. None of our URLs do not contain a slash at the end except for Home which acts as the starting point of a "Language Folder" if you imagine it like that.

Do you think it's possible that you add the option to the package or do you think it would be better to manage this in our .htaccess file?

@chinleung
Copy link
Owner

@monsieurbab What about the other routes though? Do they have a trailing slash?

If I'm not mistaken, Laravel supports the trailing slash by default.

So if your registered route is /fr, the /fr/ would work as well.

I could add an option to add a trailing slash but it would be for all routes managed by the package, not only the home url.

@monsieurbab
Copy link
Author

No, I only need it for the Home route of each language.

By default Laravel has this rule in the .htaccess:

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

If you can't add the option of trailing slash for some routes only, then I think I'll put a rule in the .htaccess for our Home routes. That will be simpler ;)

Thanks for your help!

@chinleung chinleung added the question Further information is requested label Sep 10, 2021
@chinleung
Copy link
Owner

No, I only need it for the Home route of each language.

By default Laravel has this rule in the .htaccess:

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

If you can't add the option of trailing slash for some routes only, then I think I'll put a rule in the .htaccess for our Home routes. That will be simpler ;)

Thanks for your help!

Oh I see. I use nginx which doesn't use the .htaccess, that's why it's working for me! Wouldn't that rule remove the trailing slash even if you register the route with a trailing slash though? 🤔


I found a workaround to register the home routes with a trailing slash:

Route::multilingual('home', '...');

And then in all your translation files resources/lang/{locale}/routes.php, you can add the following:

return [
    'home' => '/',
];

This will give you the following routes:

+------------------------+--------------+
| URI                    | Name         |
+------------------------+--------------+
|                        | en.home      |
| fr/                    | fr.home      |
+------------------------+--------------+

However, when you use the route helper, it will remove the trailing slash automatically:

localized_route('home', [], 'fr'); // https://demo.test/fr
route('fr.home'); // https://demo.test/fr

I think it'd be best if you alter the rules in your .htaccess to add the trailing slashes for your home route to achieve what you'd like!

Have a nice day. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants