I found this script or removing extensions from php files:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
While this works, it still allows files to be accessed if you type them in with their extension, which I don't want.
I tried the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([.]+)$ $1.php [NC,L]
I also tried ^(.*)$ and ^(.+)$
which seems like it should do the job, because it would do this:
index.php -> index.php.php
but somehow, it doesn't work as expected.
So how do I update the above .htaccess script to disallow file extensions?
EDIT:
My .htaccess script seems to be detecting the files correctly:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "other.html" [L]
RewriteRule "^([^\.]+)$" "other2.html" [L]
The pages other.html and other2.html simply contain the words "OTHER" and "OTHER 2"
Now, using the above script, the output is as expected:
"/test.php" gives output "OTHER"
"/test" gives "OTHER 2"
but if I update the script to the following, both url variations start returning "OTHER"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "other.html" [L]
RewriteRule "^([^\.]+)$" "$1.php" [L] // changed
So it seems that after the extensionless filename has ".php" added to it by rule#2, it somehow gets caught by rule #1.
Aren't these rules ordered? and isn't [L] supposed to stop processing on a match?
EDIT 2:
So assuming [L] did what I was expecting... the following script would work...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "404.html" [L]
RewriteRule "^([^\.]+)$" "$1.php" [L]
Try this solution How to remove file extension from website address?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
edit: you can do a filematch to prevent executing all .php except index.php. I typically route most of my pages through index.php anyways so this solution would work if you only have a handful of base index files (index1.php,index2.php,etc). I'm sure you can come up with something from this if you don't find a working solution.
<FilesMatch "\.php$">
Order Allow,Deny
Deny from all
</FilesMatch>
<FilesMatch "index[0-9]?\.php$">
Order Allow,Deny
Allow from all
</FilesMatch>
This works:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "404.html" [END]
RewriteRule "^([^\.]+)$" "$1.php" [END]
Related
I am trying to remove the .php extension only from the file after the first slash.
Right now I have:
example.com/file.php/123123
What I want is:
example.com/file/123123
I have tried adding this code to the .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(e)/?$ /$1.php [L,NC]
It successfully removes the .php extension but only for the last part of the URL.
Meaning it reads:
example.com/file/123123
as:
example.com/file/123123.php
and then makes it unable to load because there is no 123123.php.
How can I make it so it only tries to remove the .php from the after the first slash?
Thanks
If you have mod_rewrite module installed and enabled, you can paste the following code into your apache2.conf file:
<Directory /path/to/site/folder>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]
</IfModule>
</Directory>
This module can be installed by default, so you can just try with this code. It may work.
Before I get slapped with a "Duplicate Question," please note that I am inquiring as to how I can add on some mod_rewrite rules to my current .htaccess with the following:
Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
My issue is, I have files outside of the application directory that are not set up with a Controller. So, how can I flat-out remove all .php extensions with my current .htaccess, WHILST retaining the ability to remove the index.php (or, index) parameter from the URL?
EDIT: I have tried another RewriteCond and RewriteRule. CodeIgniter throws a 404, though, since it thinks it is a Controller.
According to Remove .php extension with .htaccess you should add
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
to your .htaccess BUT you should add it before the CodeIgniter rules like so:
Options +FollowSymLinks
RewriteEngine on
# Serve up non-CodeIgniter PHP files if they are requested without their extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
# CodeIgniter rewrite rules
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Since mod_rewrite processes and applies rules in order, you want non-CodeIgniter files to be tried first and then fall into the CodeIgniter rules.
I have no affiliation with this site and I am sure there are others like it but if you ever wanted to just test rewrite rules with various URLs then check out https://htaccess.madewithlove.be/
for removing index.php from URL in CodeIgniter. you must follow these steps.
1 . add the following code in .htaccess and .htaccess must be in the root folder.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
2 . change $config['index_page'] = 'index.php'; to $config['index_page'] = '';
you don't need to remove .php in CodeIgniter because that not exist.
if you want to learn! so use this code to remove .php extension
add this code to .htaccess
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
I know that this question has been asked a lot, but nothing I've seen so far worked.
I have a wp in root, running from subdirectory (I like things clean). I also a few subdomains running from subdirectories in root (eg. public_html/sample.com). I get the error only on subdomains that are pure HTML and CSS. They don't use any platform.
Here's my .htaccess from root
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Things I've tried:
1) Comment out all RewriteRule - solves the problem, but then my wp doesn't work. It gives 404 error when I go to any posts or pages.
2) Changed the .htaccess to the following (courtesy of Scott Yang):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php/$1 [L,QSA]
</IfModule>
but that didn't change anything.
3) I also tried commenting out and pasting new code from other questions, quite on random, but it's the same story.
Any ideas?
Cheers
I've found a solution.
In the root directory for the subdomain, in the .htaccess add the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
Rewrite Rule ^([^.]+)$ $1.php [NC,L]
This is how you can get rid of .php from the end of URL.
Replace .php with .html and it also gets rid of .html
For some reason, this fixes the problem.
Cheers
EDIT:
This solved the problem, but it didn't remove the .html from the URL. Below worked perfectly
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
Cheers
I'm trying to do a "simple" rewrite for all of my files so that you don't have to go to a link with .html at the end of the url. I've got a code that works almost completely, except for the fact that it's rewriting ALL of the files, which then screws up my index.html file and that's kind of a big problem. This is the code that I'm using:
Options FollowSymLinks
DirectorySlash Off
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.html
I guess I just need a code in there that makes it ignore index.html, so I tried this, which gave me error 403 on my index page and error 500s on every other page:
RewriteRule ^index.html$ $0 [L]
But I guess in the end, I'm not entirely sure what I'm doing and I don't know where to put the line of code, and I don't know if it's even entirely correct.
The ORIGINAL code that I tried was this but that gave me error 403s on every page:
Options FollowSymLinks
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.html
What am I doing wrong?
Try this code:
Options FollowSymLinks
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1.html -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f [OR]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^((?!index).+?)/?$ $1.html [L,NC]
I've got the following .htaccess working perfectly:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.mysite.com/mypage.php?u=$1 [NC]
This will allow me to write urls like this:
http://www.mysite.com/peter => http://www.mysite.com/mypage.php?u=peter
But what if I want have an aditional rule to avoid writing the .php extension for existing files?
http://www.mysite.com/contact (contact.php exists on file)
I tried adding this to my curr htaccess (but doesnt work):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
In other words, I would like to have vanity urls and ignore the .php extension in the same .htaccess.
SOLVED!
Thanks to Ulrich Palha and Mario (didnt know about the ordering and the LAST [L] flag!
RewriteEngine on
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.mysite.com/mypage.php?u=$1 [NC]
It's a matter of ordering. RewriteRules are applied in the order they are noted in the .htaccess
See Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?
Just add your new block as first rewrite statement, add a [LAST] flag, and it should work.
This rule works unconditionally (not guarded by any RewriteCond), so should always be the last of all RewriteRules:
RewriteRule ^(.*)$ ...
(And all preceeding RewriteRules should preferrably carry a [L] flag. You have a bit of a workaround there.)
For me this code work:
in "/etc/apache2/sites-available/default" or "httpd.conf"
you must have (at least) these options in the default directory
...
<Directory />
...
Options FollowSymLinks
AllowOverride All
...
</Directory>
...
OR in the site specific site directory
...
<Directory "/var/www/my-site">
...
Options FollowSymLinks
AllowOverride All
...
</Directory>
...
and create or edit the ".htacces" in the directory of the site (for example "/var/www/my-site/.htaccess") and add this code:
AddType application/x-httpd-php htm html php
AddHandler application/x-httpd-php .htm .html
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]