Having trouble using .htaccess for hiding the .php extension - .htaccess

I'm having trouble using .htaccess.
This is the content of my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^.]+)\.php$ $1 [L]
I opened up a text file, pasted these lines and saved it as .htaccess
It was showing .htaccess before i right clicked the .htaccess file and changed it to "open with notepad". I guess that shouldn't make a difference but now its showing a blank name.
The main problem is when i open my localhost on the browser through wamp, the folder where I've kept the .htaccess file, isn't visible or if i access it shows this internal server error.Now, if i remove the .htaccess file from there, it shows up in the localhost directory and doesn't show an error when i try to open it.

If you're getting the 500 Internal Server Error, your .htaccess file is being read. From what I see, you may be missing spaces before the !:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
#-----------------------------^^
RewriteCond %{REQUEST_FILENAME} !-f
#-----------------------------^^
RewriteRule ^([^.]+)\.php$ $1 [L]
But if you want your users not to see the .php extension, the RewriteRule is backwards. The first part should not contain the .php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# The input URL has no PHP, but internally is served as .php
RewriteRule ^([^.]+)$ $1.php [L]

Related

redirecting with htaccess causes too many redirects

The website I'm working on is using some cms. I need to add a static website to this. When I put mypage.html in the main directory and go to www.website.com/mypage.html it works. I would like the page to be accessible without '.html' ending. I experimented with editing htaccess files but always end up with error of too many redirections.
What I entered were various combinations, for example
Redirect 301 http://website.com/mypage http://website.com/mypage.html
The htaccess file I'm using looks like this:
:Location /*.php
Use php54
:Location
RewriteEngine On
DirectoryIndex index_prod.php
Options -Indexes
RewriteRule ^.*\.(css|png|swf|js|gif|jpeg|jpg|flv|pdf|doc)$ - [L]
RewriteRule ^net2ftp - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^/?$ plug.html [L]
#RewriteCond %{REQUEST_URI} !=/
RewriteRule ^/?.* index_prod.php
I'm looking for tips or to be explicitly told what and where to put in htaccess file to make it work (if it's possible)
Could you please try following, considering that you want without extension file URLs to be served by html extension files. Also since you didn't mention any specific condition before RewriteRule hence that redirection errors are coming to it, because its keep on redirecting in lack of any condition/check's presence(till its maximum redirection limit is crossed).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [NC,L]

.htaccess Rewrite Rule Understanding Problems

Site Structure
/articles/Employment/Companies.php
/articles/Employment/Companies/.htaccess
/articles/Employment/Companies/index.php
.htaccess file reads
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ index.php [L]
So when you go to
/articles/Employment/Companies/[company type]
It is displaying the index.php page.
The Problem
I'm trying to link to
/articles/Employment/Companies.php
without the .php being displayed, however if I link to
/articles/Employment/Companies
it is going to
/articles/Employment/Companies/
What i'm Ideally Looking For
Understand why I my site is adding the / when linking to folder/hello
to strip out all .php so if you go to /hello it'll display /hello.php apart from in certain directories such as my current .htaccess file is located where /this or /that will display /index.php.
Please try with below, use from rewritecond with your existing rule what I am doing if the request is actually for php file which is not index.php then serve the extension less code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule !index.php$ $1.php [L]

htaccess subfolders for html site

My htaccess code is not working correctly. Hoping to get some help. It's working perfectly unless I click a link within on of my subfolder pages. Here is my code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
I removed the .html and added a trailing slash.
When I click on a link when I am within one of my subfolder pages, it generates a weird URL.
Example: When I am at the URL: http://domainname.com/product/2-jars-500mg-hemp-extract-pain-relief-cream/
and I click on the link: href = '/product/3-jars-500mg-full-spectrum-hemp-extract-pain-relief-cream'
It rewrites the link as /product/2-jars-500mg-hemp-extract-pain-relief-cream/3-jars-500mg-full-spectrum-hemp-extract-pain-relief-cream which causes a 404 error.
I currently online have one htaccess file in the public_html folder
This site is hosted through GoDaddy on an apache server.
I tried adding the same htaccess file into the product folder, but that messes up the rewrite.
Rules looks fine, check in source code in browser your url... maybe it's relative and without slash at the beginning or end of uri ( example product/3-jars-500mg... not /product/3-jars-500mg.../ )
You need href = '/product/3-jars-500mg-full-spectrum-hemp-extract-pain-relief-cream/'
that the rule RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html would be true.

htaccess routing codeigniter

I have a problem with my routes. I have my images in sources/ folder but when the image is missing it cause to load the website and future problem with system overloading.
Htaccess:
RewriteCond $1 !^(index\.php|robots\.txt|sources|uploads|captcha|sitemap\.xml|_gapi|robots\.txt|googleaac809c6bcbeb4e8\.html|googlebaf6b56ae3013092\.html|feeds|temp)
RewriteRule ^(.*)$ /index.php?/$1 [L,QSA]
This should cause to load files from those folders - working well by this point
But when there is image missing for example:
<img src="uploads/non_existing_image.jpg"> the image wont load but when I copy the URL the website will load correctly and after some time my server ends up with 500 Error.
Is there any way how to solve this in htaccess/php ?
I want to end up with 404 error on that link
Don't use .htaccess for this, just Add the index.html file to your images folder also add the index.html in CSS and JavaScript folder. When someone try load you folder index.html is auto load and didn't allow your images etc.
It will give result like this
i also use a htaccess file with codeigniter, even though mine is quite smaller then yours:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
to explain: RewriteEngine On enables the rewriting of the url,
the RewriteCond %{REQUEST_FILENAME} !-f tells htaccess that the requested file should not be an existing file,
the RewriteCond %{REQUEST_FILENAME} !-d tells htaccess that the requested file should not be an existing directory.
this way you dont need to exclude all the seperate files and/or extentions and/or directories.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Just add this to your .htaccess file

htaccess help needed

i'm doing maintenance work on a cms and have found the following htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
i'm having trouble understanding it.
the reason a went looking for the htaccess file is this:
i placed some code in index.php (right now just printing some string to a file but
eventually will do banner cycling) and i've noticed the string gets printed a few times when i load index.php. could that have some connection to the htaccess file?
thanx in advance for any input.
This simply checks whether a file exists (as a file -f, or directory -d). If it does not, it takes the address and passes it to index.php.
For instance if you ask for:
www.mysite.com/badfile.html
You will get:
www.mysite.com/index.php/badfile.html
This should have no effect on how the code in index.php runs. This only affects what happens when non-existent files and directories are requested.
Request is for a file on the webserver, denies access to index.php
RewriteCond %{REQUEST_FILENAME} !-f
Request is for a physical directory on the webserver, denies access to index.php
RewriteCond %{REQUEST_FILENAME} !-d
Any other than the above, redirects to index.php
RewriteRule ^(.*)$ index.php/$1 [L]

Resources