1

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?

1 Answer 1

5

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]
18
  • Thank you abubhava! Although blog.php exists in the website directory, I now get the following error message: "The requested URL /blog.php was not found on this server."
    – kapantzak
    Mar 22, 2013 at 21:17
  • THat is most likely because of the other trailing slash rule. Let me suggest you something for that as well.
    – anubhava
    Mar 22, 2013 at 21:22
  • Still the same error! I have tried many combinations but I don't know what exactly I am doing..
    – kapantzak
    Mar 22, 2013 at 21:39
  • Post your latest .htaccess in your question. Also I didn't get what is createforweb_1.1? Is hat your DOCUMENT_ROOT?
    – anubhava
    Mar 22, 2013 at 21:43
  • I have edited my question with the new .htaccess. createforweb_1.1 is my DOCUMENT_ROOT. Thanks again!
    – kapantzak
    Mar 22, 2013 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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