kapantzak
kapantzak

Reputation: 11750

htaccess rewrite rule - no extension and slash

I am not familiar with .htaccess file so excuse me if my question is a little absurd.

When I request for

localhost/mydomain/blog.php

I want to have a url of this form:

localhost/mydomain/blog/

My website directory looks like that:

enter image description here

The .htaccess file includes the following:

Remove .php extension

<IfModule mod_rewrite.c>        
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
</IfModule>

With this link

<a href="blog">Blog</a>

The url is:

localhost/createforweb_1.1/blog

But I want to add a trailing slash, so if I add these lines in htaccess:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://localhost/createforweb_1.1/$1/ [L,R=301]

I get this error:

The requested URL /blog/.php was not found on this server.

I'm sure is very simple but I have a little experience in .htaccess rewrite!

LATEST .htaccess file (whole file)

<IfModule mod_rewrite.c>
 Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
 RewriteEngine On
# RewriteBase /
</IfModule>

Options -MultiViews

## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]

ALTERNATIVE SOLUTION

I just created a folder for every page, moved the files inside and renamed them into index.php. The new directory tree looks like that:

enter image description here

Now I request blog folder and blog/index.php is loading. URL becomes createforweb.gr/blog/

Is this an acceptable solution, or it will create problems in the future?

Upvotes: 1

Views: 4205

Answers (1)

anubhava
anubhava

Reputation: 785611

Put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]

Upvotes: 5

Related Questions