Home > other >  .htaccess rewrite conditon after spesific url
.htaccess rewrite conditon after spesific url

Time:06-03

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 enter image description here

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

CodePudding user response:

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 enter image description here

  • Related