removing index.php and .php extension from all pages using .htaccess - .htaccess

I had added this code in .htaccess for removing .php extension from the url's.Also I had written a code for making blog url seofriendly like https://url.com/posttitle
<IfModule mod_rewrite.c>
#removing extension from url
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#blog
#if the file with the specified name in the browser doesn’t exist, or the directory in the browser doesn’t exist then procede to the rewrite rule below
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ blog_detail.php?id=$1 [QSA,L]
</IfModule>
I had added links like Home.
Home
How to remove index from url in this case?

You can simply use a "/" in your href attribute when navigating to your root (index.html/.php)

Related

htacces doesn't rewrite url correctly

I made Ajax navigation that call pages from a /pages file in a div, and I've tried many things to remove the .php from my URLs but I can't find the solution.
Here is my .htaccess file:
Options +MultiViews +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9\-]+)\.php$ index.php?p=$1 [L]
RewriteRule ^pages/([a-z0-9\-]+)$ index.php?slug=$1
So everything works well but I can't change my URL: www.mywebsite.com/about.php to www.mywebsite.com/about
Try using this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
It should remove .php extension from the end of your URLs.

Page URL named css or js doesn't work

In my site when I go to URL such as my-site.com/css or my-site.com/js, then browser displays the folder listing instead of the actual page saved in the database. If I use Options All -Indexes in my htaccess then it says forbidden. I have following rule in my htaccess to load any dynamic page:
RewriteRule ^(.*)$ page.php?url=$1 [QSA,L]
Since I have also folders named css and js in my project, so I think server displays the directory content instead of dynamic page. How can I prevent this conflict. I know if I use a suffix "page" before my page name then it can be sorted out but i don't want to use any parameter before the page url.
EDIT
Here's my complete htaccess file:
# my-site.com
Options +FollowSymlinks -MultiViews
#RewriteBase /
#Enable mod rewrite
RewriteEngine On
DirectorySlash off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ page.php?category=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/([^/]+)$ page.php?category=$1&post=$2 [QSA,L]
Since /js and /css are real directories your rewrite rules don't execute because you have this condition:
RewriteCond %{REQUEST_FILENAME} !-d
which means don't run for directories.
You can tweak your .htaccess to this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !/$ %{REQUEST_URI}/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ page.php?category=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ page.php?category=$1&post=$2 [QSA,L]

Need help for .htaccess RewriteRule

I've been trying to setup a RewriteRule for my .htaccess to redirect any not found .php url to my loader.php ... For some reasons, it's redirecting even if the file exist.
Here's what I have:
# Engine On for Extension Removal
RewriteEngine On
# Rewrite the url for the login page
RewriteRule ^login/?$ index.php?tab=login [NC,L]
# Load the url with the page loader if the url doesn't match any of the rule above
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
# Remove PHP Extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Everything is working before I added this line:
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
Right now, no matter what is in my link, even if it exist or not, it'll load the loader.php. And I have other rules like the login one, same thing but different name which is why I didn't bother posting them.
Try
# Engine On for Extension Removal
RewriteEngine On
# Rewrite the url for the login page
RewriteRule ^login/?$ index.php?tab=login [NC,L]
# Remove PHP Extensio
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
The problem with
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
is that it accepts any .php url in the request and rewrites it to loader.php.
The condition
RewriteCond %{REQUEST_FILENAME} !-f
change this behaviour of the rule, it tells server to process the rule only when there is a request for non existent php file.

url not working with browser other than chrome, htaccess file error

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|js|css|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
I have written this .htaccess file when I access localhost/myProject/home in Chrome this works fine but in other browsers it show page not found error
Most likely you need Options +FollowSymLinks. And for more htaccess for codeigniter click on link and download zip At David Connelly's Website. They have about 5 different ones that you could try in zip, no doubt one of them will work. http://www.insiderclub.org/downloads
The one I use below works fine in all browsers for codeigniter. Make sure when you use this or any other htaccess, that you place your domain in base url at config/config.php and then remove the index.php from config/config.php
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Remove .php extension from url

I am required to remove .php extension and query string as well from url by rewriting the url
I know this can be done in .htaccess file
I have this in my .htaccess file so far
RewriteEngine on
DirectoryIndex Home.html
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.html
RewriteRule .* Home.html
now the url
example.com/blog.php?post=12&comment=3
can be accessed by
example.com/blog.php/post/12/comment/3
but i want to remove .php extension as well
this must be accessible through this url
example.com/blog/post/13/comment/3
Any help?
Thank you in advance.
Searches for everything except . after /
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

Resources