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.
Related
I am making a small cms in php. Through htaccess I would like to delete and block all extensions (.php .html. Htm etc.) and add a / at the end of the url.
ex. www.miosito/ article-name/
Options -Indexes
DirectoryIndex home.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ core.php
this is my current htaccess.
Sorry for my bad English and thank you very much in advance
With your shown samples/attempts, could you please try following htaccess Rules file. Please make sure to clear browser cache before testing your URLs.
RewriteEngine ON
##Excluding Indexes and MultiViews options here.
Options -Indexes -MultiViews
##Rule for url http://localhost:80/test here.
RewriteRule ^(test)/?$ $1/category [R=301,L]
RewriteRule ^(test/category)/?$ $1.php [NC,L]
##Rule for forbidding html OR htm OR php extension files.
RewriteRule \.(html|php|Htm)/?$ - [F,NC,L]
##Rule for adding / at end of urls.
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
##Rules for non-existing pages to be served here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ core.php [QSA,L]
Try out this code in site root .htaccess after completely clearing your browser cache:
Options -Indexes
DirectoryIndex home.php
RewriteEngine On
# block direct access to .php, .html, .html extensions files
RewriteCond %{THE_REQUEST} \.(html?|php)[?\s/] [NC]
RewriteRule . - [F]
## Adding a trailing slash
RewriteCond %{ENV:REDIRECT_STATUS ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,NE,R=301]
# core handler
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . core.php [L]
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]
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]
this seems so simple, but it's not working for me.
I am trying to have both show.php?id=$1 and producer.php?id=$1 rewrite as friendly URLs.
The first one works perfectly, the second does not. If I remove the first, the second works fine.
What am I doing wrong here?
My code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\.html$ /show.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\.html$ /producer.php?id=$1 [L]
</IfModule>
Thanks in advance!
Your problem is quite simple.
You cannot have more than one rewrite rule for the same directory, you would need to have a separate folder for each IE. have a folder called "show" and have the htaccess file that contains the rewrite data for show.
.htaccess for show folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\.html$ /show.php?id=$1 [L]
</IfModule>
Then have another folder called "producer" with the 2nd rewrite rule.
.htaccess for producer folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\.html$ /producer.php?id=$1 [L]
</IfModule>
I have the following in my htaccess files:
Options +FollowSymLink
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-F
RewriteCond %{REQUEST_FILENAME} !-D
RewriteRule (.*) index.php [L]
I am guessing the above is the reason why my images are not appearing as I am redirecting everything to index.php?
How can I just limit this to HTML files and PHP files?
Thanks all for any help
Options +FollowSymLinks
RewriteEngine on
RewriteRule \.html$ /index.php [R=301,L]