PHP file extension removal via .htaccess not working - php

Brief
Hey guys, I will qualify this post by saying I'm an .htaccess beginner and am hacking my way through this at the moment. I'm hoping this will yield a solution but also be a learning experience.
Issue
Currently, I'm having trouble removing the .php file extensions from URLs. Previously, all pages were HTML and I had successfully rewritten URLs to drop the .html extensions, but it just doesn't seem to be working for .php — I'm just getting 404 errors.
.htaccess Contents
ErrorDocument 404 http://www.example.com/404.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^{.*}$ $1.php [L,QSA]
Any education/advise you can provide would be greatly appreciated. Thanks!

Related

Transfer file requests to folder using .htaccess

I have no understanding at all on how to make/modify an htaccess rewrite rule, and I havent found anything to this specific issue.
I need to change
site.com/file.php
to
site.com/folder/file
So the .php also has to go. It needs to work for all files
Come on, there are plenty of examples for that. A little research and effort would be better.
Anyway, here is how your htaccess (in root folder) should look like
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/([^/]+)\.php(?:\s|\?) [NC]
RewriteRule ^ /folder/%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^folder/([^/]+)$ /$1.php [L]

.Htaccess Rewrites File Prevents new Web Pages from being Viewed, 404 Error

I am editing a website for a client and have hit a bit of a roadblock. I can edit and publish existing pages just fine, but if I were to create a new .php or .html page and try to view it online, I get sent to the 404 error page. I can take the new page I want and rename it to overwrite an existing page and everything loads fine.
I have tried some different ways to do the rules I need, but they either try to find the page in the root (When it's actually in /application/views/); or it works and I have the same problem as before.
I need help making a new .htaccess file that allows you to go to website.com/signup and it takes you to /application/views/signup.php. Below are some examples of what I've tried so far.
Original .htaccess:
RewriteEngine on
RewriteCond $1
!^(index\.php|images|css|js|uploads|db|fonts|blogger|support|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Another version that has the same issue.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php/$1 [L]
Lastly, a version that allows new pages to be viewed but looks for them in the root folder. Eg. The requested URL /signup.php was not found on this server. I imagine if this is modified it will work, but I have no experience with .htaccess rules. Much appreciated to anyone that can help me and future browsers!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.htm [NC,L]
You should be able to add a couple of conditions (RewriteCond directives) to the original .htaccess file rule:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|uploads|db|fonts|blogger|support|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
So, only if the request does not map to an actual file or directory will it be rewritten (to the front controller).

HTACCESS - Setting root folder as public_html

So, I'm currently writing my .htaccess file, and I've stumbled upon the issue of root folders. Currently, http://localhost/public_html/index is the root of my website. What I want to do is have the url as http://localhost/index instead.
I know this is pretty simple, but all of the posts I have looked at so far have not proven to work. Here is what I have so far, which is producing a 404 error.
RewriteEngine On
RewriteCond ${REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public_html/
RewriteRule ^(.*)$ /public_html/$1 [QSA]
Please include any documentation or guides to .htaccess itself if possible.
Thanks.
The following .htaccess code can be added to achieve the results wanted:
RewriteEngine on
RewriteCond %{REQUEST_URI} !public_html/
RewriteRule (.*) /public_html/$1 [L]

Why do I need to add index.php in order to route my website correctly? [getkirby]

I uploaded the code to the server. It started the homepage correctly . But when I press any link , I get 404 not found error. I discovered that I need to add index.php to my url for it to work.
so it will be like that:
mydomain.somee.com/myWebsite/index.php/anotherPage
When I was working locally using Xamp as a server, I didn't get any of those problems.
I got those problems after I uploaded the website to some.com which apparently doesn't use .htaccess file (editing or removing has no effect).
How to add this index.php automatically and hide it from the user?
I didn't change any of the system files or the htaccess
please tell me if you need anymore files or description.
You need to redirect all your all pages through index.php file but remove it from URL.
Write below rules in your root .htaccess file:-
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
OR
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
To understand, How htaccess rules are working, This link will help you :)
Hope it will help you :)

Link Duplicity problem with .htaccess and mod_rewrite

Sorry I couldn't come up with a better title.
Here is my htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]
And this is my problem:
localhost/validpage gives me the contents of localhost/validpage.php.
localhost/validpage/blah also gives me the same (validpage.php) page and so does localhost/validpage/blah/blah/...
Therefore the problem I am facing is link duplicity(in my words!).
How do I allow localhost/validpage.php to be accessed from localhost/validpage only and nothing else, not even localhost/validpage.php.
I have started a question on *Server****Fault*** too but with not much success.
The answer I have got is it cannot be done with htaccess alone.
By validpage I mean any valid page on the server. Since I am retrofitting an existing site with mod_rewrite for cleaner urls, I am looking for a relatively easy solution preferably with .htaccess only. However, any solutions are welcome.
what is the source attribute of your images, etc ???
absolute or relative?
<img src="/images/my.jpg" /> and <img src="images/my.jpg" /> point to different files when applying your rewrite rules.
You could try using this in your .htaccess
RewriteEngine on
RewriteBase /
# if request has a php extension remove and redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^((.*)\.php)$
RewriteRule ^(.*).php$ $1 [L,R=301]
# if request uri has no extension link to php file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I should rewrite your php scripts to friendly urls, and redirect requests using the .php extension.

Categories