How to remove extension from url WITHOUT trailing slash using .htaccess? - php

I have a .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
And I have a file called random.php.
All I want to do is just call something.com/random, but Apache adds a trailing slash (using 301 Redirect) to the end of the URL, therefore giving an error stating that something.com/random/.php cannot be found.
EDIT 1: When I use
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [NC,L]
then my external files (.js, .css, etc.) can't load and the server responds with a 500 Internal Server Error response. Apache says that there were too many redirects.

Try this rule with optional trailing slash:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

You almost had it right, you just needed to exclude the slash as well:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.\/]+)$ $1.php [NC,L]

Related

PHP rewriting URL to remove .php extension

I am working on a project where I decided to get rid of the .php extension from the URL of my app. I am successful using the htaccess code given below. But the problem is, the page still loads using .php extension if typed manually or loaded from the history.
My current .htaccess code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I want it to always load with www.example.com/dashboard except www.example.com/dashboard.php even if dashboard.php is manually typed.
UPDATED .htaccess code
RewriteEngine On
RewriteBase /dateapp/
RewriteRule ^([^\.]+).php$ $1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,END]
You can add redirect as first rule.
RewriteEngine On
RewriteRule ^([^\.]+).php$ /$1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,END]
You have to use END flag instead of L otherwise the redirects will end in infinite loop. This will work if the .htaccess is in your webroot folder.
In case you have .htaccess in 'folder' subfolder you will have to add RewriteBase
RewriteEngine On
RewriteBase /folder/
RewriteRule ^([^\.]+).php$ $1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,END]
You may use this code in dateapp/.htaccess:
RewriteEngine On
RewriteBase /dateapp/
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Why does my .htaccess file cause an internal error to some files when adding a slash? [duplicate]

I have a .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
And I have a file called random.php.
All I want to do is just call something.com/random, but Apache adds a trailing slash (using 301 Redirect) to the end of the URL, therefore giving an error stating that something.com/random/.php cannot be found.
EDIT 1: When I use
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [NC,L]
then my external files (.js, .css, etc.) can't load and the server responds with a 500 Internal Server Error response. Apache says that there were too many redirects.
Try this rule with optional trailing slash:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
You almost had it right, you just needed to exclude the slash as well:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.\/]+)$ $1.php [NC,L]

.htaccess not redirecting to .php files

I need to redirect the following pages to their .php versions.
http://www.kgolf360.com/about
http://www.kgolf360.com/news
http://www.kgolf360.com/junior-performance-programs
http://www.kgolf360.com/before-after
http://www.kgolf360.com/book-lesson
http://www.kgolf360.com/family-performance-programs
http://www.kgolf360.com/technology
http://www.kgolf360.com/adult-performance-programs
For example, I need to redirect
http://www.kgolf360.com/about
to
http://www.kgolf360.com/about.php
etc.
What I did is modified my .htaccess file by adding the following:
Redirect 301 /about http://www.kgolf360.com/about.php
but it gives me a 404 error. You can see it if you go to http://www.kgolf360.com/about
Is it possible that I need to configure something else to make it work? If so, what exactly do I need to do to redirect the URLs properly?
Here's the content of my .htaccess file:
rewriteengine on
rewritecond %{HTTP_HOST} ^www.kgolf360.com$ [OR]
rewritecond %{HTTP_HOST} ^kgolf360.com$
rewriterule ^about$ "http\:\/\/kgolf360\.com\/about\.php" [R=301,L] #5682a68c22ecf
rewritecond %{HTTP_HOST} ^kgolf360.com$
rewriterule ^(.*)$ "http\:\/\/www\.kgolf360\.com\/$1" [R=301,L] #567eeb84dcae2
# The following will allow you to use URLs such as the following:
#
# example.com/anything
# example.com/anything/
#
# Which will actually serve files such as the following:
#
# example.com/anything.html
# example.com/anything.php
#
# But *only if they exist*, otherwise it will report the usual 404 error.
Options +FollowSymLinks
# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html
# Redirect to PHP if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.php
rewritecond %{REQUEST_FILENAME} !-f
rewritecond %{REQUEST_FILENAME} !-d
rewritecond %{REQUEST_FILENAME}.html -f
rewriterule ^(.+)$ $1.html [L,QSA]
rewritecond %{REQUEST_FILENAME} !-f
rewritecond %{REQUEST_FILENAME} !-d
rewritecond %{REQUEST_FILENAME}.php -f
rewriterule ^(.+)$ $1.php [L,QSA]
#Redirect all non-php URLs to their php versions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Do you guys see any issues with the content I have in the fle now?
Since you are using Godaddy, you need to use Options +MultiViews. So try this code:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Weird .htaccess behaviour

this is my .htaccess-content:
RewriteEngine On
RewriteBase /
Options -Indexes -MultiViews
#Rewriting /profile.php?name=XY to /player/XY
RewriteRule ^player/([^/]*)$ /profile.php?name=$1 [L]
#Remove .php file ending
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
If I am browsing to my-domain/player/XY it redirects me to player.php?name=XY (and prints an internal server error because player.php doesn't exist) instead of showing the profile.
But if I change it to
RewriteRule ^player/([^/]*)$ /profile.php?name=$1 [L] and open my-domain/playera/XY it works fine.
Can you help me, please?
Not sure why you're getting that error since the first rule should match /player/XY. But you can probably add a few conditions to your php rule to ensure that it is rewriting correctly:
#Remove .php file ending
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]

URL doesn't work with slash after removing extension using htaccess

I have been working on localhost, and my htaccess file is
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
after adding the htacces code,the url
localhost/movies/news.php
works
localhost/movies/news
also works but
localhost/movies/news/
doesn't work. It shows "Internal Server Error".How to make it work with slash and without slash.
You an try this code:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Internally forwards movies/news/ to movies/news.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
The problem is when you add the slash you have news/.php and this is not working.
A better solution is to rewrite to a GET variable something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?url=$1 [L]
Then you can filter the GET variable in your script and include the file or content you need.

Categories