0

I'm using apache2 and laravel so I put this code inside .htaccess on /public folder

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} ^/?(blog|dont/you|brave-to|fvck|me)$ 
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

code above is working only if url spesific with the /url

An example https://mywebsite.com/blog or https://mywebsite.com/dont/you

so with this RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L] will redirect you to get tailling slash

so will be https://mywebsite.com/blog/

enter image description here

But when I want to add dynamic slug this not working enter image description here link test

I mean, I need to add tailling slash after end of slug following by blog an example if the url like this https://mywebsite.com/blog/hey-this-dynamic-slug this should be https://mywebsite.com/blog/hey-this-dynamic-slug/

I have to add this condition and rule

RewriteCond %{THE_REQUEST} !\s/(?:blog/)\S+\s [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

yes sure working fine enter image description here

https://htaccess.madewithlove.com?share=47516f5c-b5c6-49e9-87fa-3b83cec864fc

but this will force all with / image

as you can see when I try to change blog with random text this rule force to add /

I just need to force add tailling slash only for spesific url and after spesific url

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
AdityaDees
  • 1,022
  • 18
  • 39
  • What does this have to do with `Laravel`? You should never modify the `.htaccess`, what are you using? – matiaslauriti May 25 '22 at 01:51
  • 1
    what do you mean? don't modify `.htaccess` ? @matiaslauriti – AdityaDees May 25 '22 at 02:15
  • I am going to say it again, why do you need a trailing slash at the end of a url? – matiaslauriti May 25 '22 at 03:21
  • I am asking this because it is not something everyone needs, specially with Laravel, so you are either doing something wrong, or you have a good case, so please share the why, because we will learn either way... it is also on the [ask]... you need to share information... – matiaslauriti May 25 '22 at 03:30
  • For self site maybe this not important, but when you care about SEO or Web Structure you need to do this. I'm still thinking why when we use cms like wordpress/blog they give you option for how `Permalinks` will appear on your site? They must not give this option if they dont take care about this. Also our community open threads [When Should I use a trailing slash](https://stackoverflow.com/questions/5948659/when-should-i-use-a-trailing-slash-in-my-url) or [Why trailing slashes on URIs are important](https://cdivilly.wordpress.com/2014/03/11/why-trailing-slashes-on-uris-are-important/) – AdityaDees May 25 '22 at 03:44
  • 1. laravel have default .htacess and will remove `/` at the end (I'm done with this) 2. if you take care about route may you need to know if `Route::post('/testurl/')` with `Route::post('/testurl')` will have different effect. thats why I need to do this for only spesific url. like I say before the .htacess is working fine, but what I want is for spesific url and after spesific url. this important for handling the route post – AdityaDees May 25 '22 at 04:03

1 Answers1

0

OK So i got it use this condition

RewriteEngine On
// check if not a file
RewriteCond %{REQUEST_FILENAME} !-f 
// check if there start with blog/ and not end with /
RewriteCond %{REQUEST_URI} ^/(blog/.*)[^/]$ [OR]
// OR check if contain specific words
RewriteCond %{REQUEST_URI} ^/(blog|dont/you|brave-to|fvck|me)$
// the do the rule
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

this an example

enter image description here

AdityaDees
  • 1,022
  • 18
  • 39