.htaccess - hidding .php and forcing trailing slash not working - php

I'm trying to hide .php extension from my website while forcing trailing slash, I've been searching for the last few days with no success.
The site is running in a XAMPP server currently.
Here's the .htaccess file:
Options -Indexes -Multiviews +FollowSymlinks
RewriteEngine On
RewriteBase /
#removing .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
#forcing trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
ErrorDocument 404 http://localhost/404/
These links work:
localhost/about-us redirects to localhost/about-us/ which is correct but when I try to access localhost/about-us.php, it redirects to localhost/about-us/ instead of localhost/about-us/. (sorry can't post links)
How can this be fixed?

You just seem to have accidentally used an absolute path. Just remove the slash at the beginning of /$1/ like your other rules to make it work properly.
#removing .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ $1/ [L,R=301]

Related

Removed .php extension but now it says page does not exist

I was able to get the .php off my files but now when I go to the pages they do not exist(and it's forcing a WordPress 404 page when the target files are actually on the server outside of WordPress). I think the problem might be that it's on a server with WordPress and these files lie outside of word press. There's already rewrite rules for WordPress and maybe it's conflicting with my rewrite rules for other files?
.htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Remove .php extension on files outside of wordPress
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
# Force trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
UPDATE - Full .htaccess
# BEGIN WP Hide & Security Enhancer
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#WriteCheckString:1548706048_68906
RewriteRule .* - [E=HTTP_MOD_REWRITE:On]
RewriteRule ^site(.*) /wp-login.php$1 [L,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^wp-login.php /index.php?wph-throw-404 [L]
RewriteCond %{REQUEST_URI} /enter$
RewriteRule ^(.*)$ /enter/ [R=301,L]
RewriteRule ^enter(.*) /wp-admin$1 [L,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^wp-admin(.+) /index.php?wph-throw-404 [L]
</IfModule>
# END WP Hide & Security Enhancer
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You need to do something like this
#remove all PHP extensions from the url
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301] #end and redirect
# Force trailing slash ( probably no longer needed, because `/$1/` )
#RewriteCond %{REQUEST_FILENAME}.php -f
#RewriteRule .*[^/]$ $0/ [L,R=301]
#add .php back in and continue.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php #no last flag as we can now continue to WP with our nice php extensions back on
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I'm assuming those rewrite rules are correct. I would have to look them up I haven't worked heavily with Rewrites in a few years. Basically
first remove any php extensions and redirect (if they exist)
on a redirect (or a request without .php) add back the php extension to those urls where it was removed so you may need %{REQUEST_FILENAME} !-d just to be safe.
carry on as normal
Just as a general rule of thumb, it's best to do redirects first. That way you can get to the redirect with the shortest amount of work. A redirect is like changing the url and then putting that in the browser and running it again (ie. a brand new request).
Another way to do this is to redirect all requests to a certain folder to an index.php of your making that has a router.
Such as this one (that I made as an example)
https://github.com/ArtisticPhoenix/MISC/tree/master/Router
Then your URL's can be like this
http://yourdomain.com/somedir/index.php/controller/method/...args
#or without the index.php
http://yourdomain.com/somedir/controller/method/...args
Then in HTACCESS all you would do is remove index.php
#remove index.php and redirect
RewriteRule somedir/index.php/(.+)/?$ /somedir/$1/ [L,R=301] #end and
#add index.php back in
RewriteRule somedir/(.+)/?$ /somedir/index.php/$1/ [L]
//wordpress stuff
The nice thing about routers is you never have to touch Htaccess, and with a folder name to key in on it should make things simpler.
Using the router and the example files:
http://yourdomain.com/somedir/user/login
Should go to Controllers/user.php method login.

5 .htaccess Rewrites: Force HTTPS, Remove index.php, Remove .php, Force www, Force Trailing Slash

I've read many articles and cannot seem to get ALL of the combined .htaccess Rewrites to work together. I either get re-direct loops or one or a few do not work at all.
To be clear I'm looking for the following 5 to occur if needed:
Ensure www on all URLs
Ensure HTTPS for all version of site
Remove index.php from url
Remove all .php extension / Re-direct to url without .php extension
Could be in tandem with the previous: add trailing slash
Some examples:
example.com/index.php => https://www.example.com/
www.example.com/info/really-good-info.php => https://www.example.com/info/really-good-info/
www.example.com/about-us.php => https://www.example.com/about-us/
www.example.com/careers/index.php => https://www.example.com/careers/
Here is current .htaccess setup:
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
# Remove all .php extensions without interfering with .js or .css.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.(?!js|css)([^.]*)$ $1\.php
# Remove index from url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
# Ensure www on all URLs.
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Ensure all URLs have a trailing slash.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ https://www.example.com/$1/ [L,R=301]
</IfModule>
The above .htaccess is ONLY in my root folder and currently does 3 out of the 5 needed: changes to HTTPS, adds www and removes index.php. It does not remove any other .php files extension nor does it add trailing slash.
I see 2 issues:
Redirect rules appearing after rewrite ones
Adding .php should only happen after you ensure corresponding .php file exists.
Have it this way:
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
# Ensure www on all URLs.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=302]
# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
RewriteCond %{THE_REQUEST} \s/*(.*?)/index\.php [NC]
RewriteRule ^ %1/ [R=302,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]
# Ensure all URLs have a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^.]*?[^/.]$ %{REQUEST_URI}/ [L,R=302]
# Remove all .php extensions without interfering with .js or .css.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
# Remove index from url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+?)/?$ index.php?$1 [L,QSA]
Make sure to clear your browser cache before testing this.
Try this to avoid the loop:
#non-www. http to www. https
RewriteCond %{ENV:HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?yourdomain\.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
#non-www. https to www. https
RewriteCond %{ENV:HTTPS} on
RewriteCond %{HTTP_HOST} ^yourdomain\.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/*(.*?)/index\.php [NC]
RewriteRule ^ %1/ [R=302,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]
# Ensure all URLs have a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^.]*?[^/.]$ %{REQUEST_URI}/ [L,R=302]
# Remove all .php extensions without interfering with .js or .css.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
# Remove index from url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+?)/?$ index.php?$1 [L,QSA]

Remove .php from URL

Ubuntu 14.04LTS 32bit
LAMP
I know it's an old question but..
I need it to remove .php anywhere it finds it from the visible url.
It needs to work with /showthread.php?id=XX ---> /showthread?id=XX
I can't even get it to work with /page.php --> /page.
I've tried these:
Remove .php extension with .htaccess
How to hide the .html extension with Apache mod_rewrite
Remove .php from urls with htaccess
How to stop .htaccess loop
It just does nothing at all.
While other .htaccess code works fine..
While
<?php
phpinfo();
Lists mod_rewrite in Loaded Modules
And
<?php
if(!function_exists('apache_get_modules') ){ phpinfo(); exit; }
$res = 'Module Unavailable';
if(in_array('mod_rewrite',apache_get_modules()))
$res = 'Module Available';
?>
<html>
<head>
<body>
<p><?php echo apache_get_version(),"</p><p>mod_rewrite $res"; ?></p>
</body>
</html>
Returns Module Available
Tried many more things
# 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>
#
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
Not even this has any effect whatsoever:
RewriteRule ^page$ page.php [L]
sudo service apache2 restart does not change anything.
Server reboot changes nothing.
I tried clearing other code inside, did not make any change.
I cleared my browser cache 100 times
I'm starting to think that it just hates me. What could possible be causing this??
Hope helped.
It's worked for me.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# For LocalHost !.php
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=::1
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Check this link.
This is the answer using .htaccess:
RewriteEngine On
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
Tested it on Windows with WAMP and working.
try this to remove .php extensions completly from your file and to avoid infinite loop:
RewriteEngine On
RewriteBase /
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]
This code will work in Root/.htaccess, Be sure to change the RewriteBase if you want to place this to a htaccess file in sub directory.
You may be on the right track. However, it sounds like your .htaccess file is not being executed. Just because a module is activated, does not mean it is available for you in your particular situation.
Here are some steps to solve your issue:
First of all, check the spelling very carefully. Verify that it is spelled correctly (including the . at the beginning)
Check the file permissions. Some servers are going to require executable permissions. So chmod the file to 755.
If you still do not have it working, go into your apache configuration (probably at /etc/apache2/apache2.conf on Ubuntu) and find every instance of AllowOverride. It might be set to 'none'. Change this to AllowOverride all instead.
Then go into sites-enabled, find your site configuration, and change the AllowOverride fields there are well.
Restart your Apache server and congratulate yourself with a big cup of coffee.
One of these should fix it. I would recommend trying between each step so that you can pinpoint where the error occurred. After determining the cause, you may want to go back and restrict some of those AllowOverrides, depending on your needs.
Best of luck!
You can try the following.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php$2 [L]
It works for me:
#On rewrite
RewriteEngine On
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite domain.com/username.php to username
RewriteRule ^([A-Za-z0-9_-]+)/?$ $1.php [NC,L]
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/?$ $1/$2.php [NC,L]
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ $1/$2/$3.php [NC,L]
And more:
Look at my rewrite file for social portal like fb:
# allow only from ip range /8 /16 /24 /31
#Order deny,allow
#Deny from all
#Allow from 89.230.0.0/16
# On rewrite
RewriteEngine On
# [NC]- case-insensitive
# [L] - Last Rule , stop the rewriting process here
# [OR] = Or - If it matches this condition or the next
# [QSA] - Append query string to rewriting url
# [R] - redirect [R=301] move permanently, 302 - temporarily, 403 - Forbidden, 404 - Not Found, 410 - Gone
# fix folder redirect images and css js/images/css/media
RewriteRule ^.+?/((img|css|js|media|upload|posts)/.+)$ /$1 [L,R=301,NC]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# does not work on IIS windows server url rewrite when importing
# RewriteCond %[REQUEST_FILENAME] !-l
# rewrite example.xx/index.php na example.xx/
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index\.(php|html?)$ / [R=301,NC,L]
#portfolio rewrite folder rewrite
# RewriteRule ^([portfolio]+)/?$ /portfolio/index.php?id=$1 [NC,L]
# pretty url example.xx/id/post/number
# rewrite domain.com/username like twitter or facebook users
RewriteRule ^([A-za-z0-9_-]+)/?$ index.php?username=$1 [NC,L]
# domain.com/post/name
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/?$ profil.php?id=$1&menu=$2 [NC,L]
# domain.com/cat/post/id
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/([0-9]+)/?$ profil.php?id=$1&menu=$2&page=$3 [NC,L]
#RewriteRule ^([A-Za-z0-9_-]+)/(ustawienia)/?$ ustawienia.php?id=$1&menu=$2 [NC,L]
#RewriteRule ^([A-Za-z0-9_-]+)/(wpisy)/?$ profil0.php?id=$1&menu=$2 [QSA,NC,L]
#RewriteRule ^([A-Za-z0-9_-]+)/(fani)/?$ profil1.php?id=$1&menu=$2 [QSA,NC,L]
#RewriteRule ^([A-Za-z0-9_-]+)/(ogladasz)/?$ profil2.php?id=$1&menu=$2 [QSA,NC,L]
#RewriteRule ^([A-Za-z0-9_-]+)/(zdjecia)/?$ profil3.php?id=$1&menu=$2 [QSA,NC,L]
#RewriteRule ^([A-Za-z0-9_-]+)/(video)/?$ profil4.php?id=$1&menu=$2 [QSA,NC,L]
#RewriteRule ^([A-Za-z0-9_-]+)/(ulubione)/?$ profil5.php?id=$1&menu=$2 [QSA,NC,L]
#RewriteRule ^([A-Za-z0-9_-]+)/(wiadomosci)/?$ profil6.php?id=$1&menu=$2 [QSA,NC,L]
#RewriteRule ^([A-za-z0-9_-]+)/?$ profil.php?id=$1 [NC,L]
#RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/?$ profil.php?id=$1&menu=$2 [NC,L]
#RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ index.php?id=$1&dir=$2&post=$3 [NC,L]
# Redirect all subdomains
# RewriteCond %{HTTP_HOST} ^(.*)\.breakermind\.com
# RewriteRule ^(.*)$ http://breakermind.com/$1 [R=301,QSA,NC,L]
# Redirect all subdomains
# RewriteCond %{HTTP_HOST} ^www.ns2.breakermind.com [NC]
# RewriteRule ^(.*)$ http://breakermind.com/$1 [R=301,QSA,NC,L]
# RewriteCond %{HTTP_HOST} ^ns2.breakermind.com [NC]
# RewriteRule ^(.*)$ http://breakermind.com/$1 [R=301,QSA,NC,L]
# Redirect from www. to non-www (unhash if need)
# RewriteCond %{HTTP_HOST} ^www.breakermind.com [NC]
# RewriteRule ^(.*)$ http://breakermind.com/$1 [R=301,QSA,NC,L]
# Redirect from http:// to https:// (from no-ssl to ssl)
#RewriteCond %{SERVER_PORT} 80
#RewriteRule ^(.*)$ https://breakermind.com/$1 [R=301,QSA,NC,L]
ErrorDocument 400 /er404.html
ErrorDocument 401 /er404.html
ErrorDocument 402 /er404.html
ErrorDocument 403 /er403.html
ErrorDocument 404 /er404.html
ErrorDocument 500 /er404.html
ErrorDocument 502 /er404.html
ErrorDocument 504 /er404.html
#RewriteEngin On
#RewriteCond %[REQUEST_FILENAME] !-d
#RewriteCond %[REQUEST_FILENAME] !-f
#RewriteCond %[REQUEST_FILENAME] !-l
#RewriteRule ^(.+)$ index.php?c=$1 [QSA,L]
#
# \w = [A-Za-z0-9_] \d = [0-9]
And here from my blog system:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index\.(php|html?)$ / [R=301,NC,L]
# category
RewriteRule ^category/?$ index.php?id=0&page=0 [NC,L]
RewriteRule ^category/([A-Za-z0-9]+)/?$ index.php?id=$1&page=0 [NC,L]
RewriteRule ^category/([A-Za-z0-9]+)/([0-9]+)/?$ index.php?id=$1&page=$2 [NC,L]
RewriteRule ^category/([A-Za-z0-9]+)/([0-9]+)/([A-Za-z0-9]+)/?$ index.php?id=$1&page=$2&title=$3 [NC,L]
# autor
RewriteRule ^autor/?$ index.php?id=0&page=0 [NC,L]
RewriteRule ^autor/([A-Za-z0-9]+)/?$ index.php?id=$1&page=0 [NC,L]
RewriteRule ^autor/([A-Za-z0-9]+)/([0-9]+)/?$ index.php?id=$1&page=$2 [NC,L]
# article, gallery, tags
RewriteRule ^article/([A-Za-z0-9]+)/?$ article.php?id=$1 [NC,L]
RewriteRule ^gallery/([A-Za-z0-9]+)/?$ gallery.php?id=$1 [NC,L]
RewriteRule ^tags/([A-Za-z0-9]+)/?$ tags.php?id=$1 [NC,L]
RewriteRule ^archive/([0-9]+)/([0-9]+)/?$ archive.php?year=$1&month=$2 [NC,L]
# custom page rewrite
RewriteRule ^page/([A-Za-z0-9]+)/?$ page.php?id=$1 [NC,L]
# fix folder redirect images and css js/images/css/media
RewriteRule ^.+?/((admin|img|css|js|media|upload|posts)/.+)$ /blog/$1 [L,R=301,NC]
# user or category user.php
# RewriteRule ^([A-Za-z0-9]+)/?$ index.php?user=$1 [NC,L]
# example
# RewriteRule ^([folder])/([category])/([A-Za-z0-9]+)/?$ index.php?id=$3&cat=$2 [NC,L]
Assuming that .htaccess is being processed, then this super simple .htaccess should work for you:
RewriteEngine on
RewriteRule (.*) $1.php [END]
If it doesn't work, there is something wrong with your Apache configuration.
You need to look into that first.
If it works, add the following line before the RewriteRule to allow serving other files:
RewriteCond %{REQUEST_FILENAME} !-f
The END flag is available since Apache 2.3.9.
RewriteEngine on //replacement launch
RewriteCond %{REQUEST_FILENAME} !-d //If the requested object is not a folder
RewriteCond %{REQUEST_FILENAME}\.php -f //If the requested object to append the extension php - file
RewriteRule ^(.*)$ $1.php //make the change from concatenating .php
Enabling mod_negotiation in my Apache config did the trick for me:
Content negotiation, or more accurately content selection, is the selection of the document that best matches the clients capabilities, from one of several available documents.
It's working, You can try this code.
1. First create .htaccess file in your main directory and paste the same code after that clear your history and its working.
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Maybe this could be a duplicate, but take a look at this:
How to remove .html from URL
In the solution, just change html to php

.htaccess remove .php and force trailing slash

I have files in my root folder. jobs.php, contact.php.
I want to be able to use .htaccess to remove the .php extension and force a trailing slash /. So that, www.domain.com/jobs/ and www.domain.com/contact/ goes to their respective pages.
Also, I want jobs.domain.com to point to www.domain.com/jobs/
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteRule ^(rosquillos)/$ $1 [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
This is what I've got so far, and as much as I try. I can't seem to get it to work.
Any help would be awesome.
EDIT:
I would like to rephrase my question. The existing code removes the .php extensions for me, what I really only need right now is an additional rule that points the subdomain to the correct file: ie. jobs.domain.com points to jobs.php. I can live without the trailing slash.
Your condition:
RewriteCond %{REQUEST_FILENAME}\.php -f
will fail if your URL's end with a trailing slash. But if you only have jobs and contact then you'll just want:
RewriteEngine On
# add trailing slash and redirect browser
RewriteRule ^([^/]+)$ /$1/ [L,R=301]
RewriteRule ^([^/]+)/$ /$1.php [L]
# redirect direct access to php files:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)\.php
RewriteRule ^ /%1/ [L,R=301]
Oh and I forgot:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
Assuming that *.domain.com has the same document root as www.domain.com.

Internal Server Error when Page Doesn't Exist

I've just realised that if you were to manually type in the URL bar a page that doesn't exist on my site, the site responds with an Internal Server Error.
Should it not be a 404 page not found?
My site is http://www.cristianrgreco.com, for example http://www.cristianrgreco.com/example will return a server error.
This is an error I frequently encountered when toying with .htaccess files so I have posted mine below.
RewriteEngine On
Options +FollowSymLinks
# Add WWW to URL
RewriteCond %{HTTP_HOST} ^cristianrgreco\.com$ [NC]
RewriteRule ^(.*)$ http://www.cristianrgreco.com/$1 [L,R=301]
# Remove trailing slashes from end of URL
RewriteCond %{HTTP_HOST} !^\.cristianrgreco\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]
# Rewrite article URLs
RewriteRule ^articles/([a-zA-Z0-9_-]+)/?$ articles.php?article=$1
# Remove file extension from PHP files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
Thanks in advance for your help.
I have encountered this problem before, the problem is this line
RewriteRule ^(.*)$ $1.php [L,QSA]
replacing it with RewriteRule ^([^\.]+)$ $1.php [NC,L,QSA] seems todo the trick. the enclosing brackets signify that anything that is matched will be remembered by the RewriteRule. Inside the brackets, it says “I’d like one or more characters that aren’t a dot”.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
# Add WWW to URL
RewriteCond %{HTTP_HOST} ^cristianrgreco\.com$ [NC]
RewriteRule ^(.*)$ http://www.cristianrgreco.com/$1 [L,R=301]
# Remove trailing slashes from end of URL
RewriteCond %{HTTP_HOST} !^\.cristianrgreco\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]
# Rewrite article URLs
RewriteRule ^articles/([a-zA-Z0-9_-]+)/?$ articles.php?article=$1
# Remove file extension from PHP files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L,QSA]
</IfModule>

Categories