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]
1

'Fake folders', or 'permalinks', are often used by Wordpress.  You set these up from your WP back end in Settings->Permalinks, where you have several choices of how these display in the URL bar. All the 'pretty' options incorporate a trailing slash, eg. http://your_site.com/sample-post/ - normally this doesn't make any difference to how fast a page loads.  However, if you want to index into a page named 'target-post' having put an <a id="index">Link</a> into that page, if the link from http://another_site.com is of the form <a href="http://your_site.com/target-post#index">, Wordpress will add the trailing slash - result is the index won't work.

The way out of this is to select Custom Structure in the WP back end and http://your_site.com/%postname% - ie. NO trailing slash.  Indexed links will then work.

As far as page loading performance is concerned, looking at the logs I got a 301 (redirect) if I manually put in the URL WITH the slash and a 200 (success) WITHOUT the slash after I had made the change.  Previously (ie. letting Wordpress put in the slash), I got a 301 WITHOUT it a 200 WITH it.

So the bottom line is, in Wordpress, it doesn't matter whether the 'permalink' includes the slash or not - UNLESS you want to use indexed links into other pages.

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.