1

The working codes that I've found here most put a slash in everything.

I just need force trailing slash in these "fake dirs" (that are part of my rewriting scheme (only directories)) and keep everything else (fake files ending with .html) as it is.

Here is my .htaccess:

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www.example.com [nc]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

DirectoryIndex index.php?category=1

Options -Indexes

RewriteRule ^album/([0-9]+)$ /index.php?p=$1&opc=4
RewriteRule ^album/$ /index.php?p=1&opc=4

RewriteRule ^album/(.*)-(.*).html /view.php?id=$2&lang=$1

RewriteRule ^search/(.*)/$ /index.php?lang=en&search=$1  [L,QSA]
RewriteRule ^channels/(.*)/$ /index.php?lang=en&channel=$1  [L,QSA]
RewriteRule ^(.*)/favorites/$ /index.php?lang=en&favorites=$1  [L,QSA]
RewriteRule ^(.*)/likes/$ /index.php?lang=en&likes=$1  [L,QSA]
RewriteRule ^(.*)/albums/(.*)/$ /index.php?lang=en&albumid=$2|$1  [L,QSA]
RewriteRule ^(.*)/albums/$ /index.php?lang=en&album=$1  [L,QSA]
RewriteRule ^albums/$ /index.php?lang=en&album=all  [L,QSA]

RewriteRule ^newest/$ /index.php?opc=1&lang=en  [L,QSA]
RewriteRule ^popular/$ /index.php?opc=2&lang=en  [L,QSA]
RewriteRule ^best/$ /index.php?opc=3&lang=en  [L,QSA]

RewriteRule ^categories/$ /index.php?category=1  [L,QSA]
RewriteRule ^categories/([0-9]+)$ /index.php?p=$1&category=1  [L,QSA]
RewriteRule ^categories/(.*)/$ /index.php?lang=en&categories=$1  [L,QSA]
RewriteRule ^categories/(.*)/([0-9]+)$ /index.php?lang=en&categories=$1&p=$2  [L,QSA]
  • 1
    What is a "fake" folder? What is the difference between a fake folder and a real folder to you? – Stephen Ostermiller Apr 3 '18 at 13:46
  • "most put a slash in everything." - What do you mean by this? None of the directives you've posted above "put a slash in anything"? However, most of these directives are expecting a trailing slash at the end of the requested URL - is that what you are referring to? But, how are you wanting to "force" this? With a redirect? Or simply allow access to the URL with and without the trailing slash? – MrWhite Apr 3 '18 at 14:36
  • 1
    @StephenOstermiller I mean by "fake folder" a dynamic folder... the file and folder doesn`t exist on server..... – Jeferson Cardozo Apr 3 '18 at 18:39
  • 1
    @MrWhite The script generates pretty urls with folders and .html, (yes pretty urls with .html) but when you try to access this folder without trailing slashs, ( sample: www.sample.com/folder ) it shows a 404 page, it only works with / at the end..... I am try to force these folders with trailing-slash at the end with some kind htaccess redir – Jeferson Cardozo Apr 3 '18 at 18:45
2

You will need to redirect to add the slash. This will redirect any set of letters without other punctuation such as slashes or dots in them. I don't think it conflicts with any of your other rewrite rules.

RewriteRule ^((.*/)?[a-z]+)$ http://www.example.com/$1/ [R=301,L]

You could make it a bit safer by making sure it doesn't match existing files on disk. To do so prepend these two conditions:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Alternately, you could put in separate rules for each of your "fake" directories:

RewriteRule ^(album|categories|newest|best)$  /$1/ [R=301,L]
RewriteRule ^((search|categories)/[^/]+)$  /$1/ [R=301,L]
RewriteRule ^(.*/(favorites|likes|albums))$  /$1/ [R=301,L]
RewriteRule ^(.*/albums/[^/]+)$  /$1/ [R=301,L]

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.