0

I'm using this code

 # Apache Rewrite Rules
 <IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /

# Add trailing slash to url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^([^\.]+)/$ $1.php 

# End of Apache Rewrite Rules
 </IfModule>

It doesn't hide .php extensions from my pages, when I remove '.php' from my anchor tags, the page get redirected. For eg. if url is www.example.com/mysite/store.php and when I remove '.php' from URI or anchor href, the new url becomes www.example.com/store/ which is not available off course. I tried to manually enter url as example.com/mysite/store but it is unavailable too.

Apparently .htaccess is not using these rules, if I write gibberish there, get a 500 server error, so .htaccess is working fine but what wrong is I'm doing.

Advice. Thanks in advice!

Update

Okay, I've figured it out, I successfully achieved what I asked in question. But my bootsrap is completely broken, I guess because it is linking 'bootstrap.css' as 'bootstrap.css.php' because the .htaccess file I'm now using is :

# Turn on the rewrite engine
RewriteEngine  on
# If the request doesn't end in .php (Case insensitive) continue processing rules
RewriteCond %{REQUEST_URI} !\.php$ [NC]
# If the request doesn't end in a slash continue processing the rules
RewriteCond %{REQUEST_URI} [^/]$
# Rewrite the request with a .php extension. L means this is the 'Last' rule
RewriteRule ^(.*)$ $1.php [L]

DefaultType application/x-httpd-php
DirectoryIndex index.php index.html

Update 2

Sorry, this was not the problem as I mentioned in last update as after I stripped the bootstrap links in my index.php, I got different results. The problem is: localhost/example/index.php is my homepage but localhost/example is what is shown in url. localhost/example is a directory with files in it. So images that should be loaded as localhost/example/image/logo.png is loaded as localhost/example/index.php/image/logo.png making example site behave weird.

innocent rock
  • 129
  • 10

1 Answers1

1

Check already posted answers:

Remove .php extension with .htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Community
  • 1
  • 1
Sid
  • 560
  • 7
  • 17
  • Thank you @Sid, I got this to work, apparently. Just one thing, when I click the link, this .htaccess just removes the appended .php in url, thus page is not found error is displayed. Any idea how to get that to work. Thanks again. – innocent rock May 06 '16 at 06:47
  • Okay, I've figured it out, I successfully achieved what I asked in question. But my bootsrap is completely broken, I guess because it is linking 'bootstrap.css' as 'bootstrap.css.php' because the .htaccess file I'm now using is : (See Update in Question) – innocent rock May 06 '16 at 06:59
  • I have added condition to ignore css, js and image files...hope this helps – Sid May 06 '16 at 10:37