Removing .php extension from URL - php

I have this code for removing the .php extension from a URL like this: domain.com/file.php to this: domain.com/file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Is there any way to remove the .php extension from domain.com/file.php?action=123456 to domain.com/file?action=123456 ?
Any help would be appreciated!

Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule (.*) $1.php [L,QSA]

Try this rule :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} /file\.php\?action=([^&\s]+) [NC]
RewriteRule ^ file?action=%1 [NC,L,R]

Okay, for those who are wondering, I basically answered my own question. My original code works for all. :)
Answer:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Related

Not Found when rewriting URL

So I have an article.php and what I'm trying to do is instead of article.php?id=1. I'm trying to do article/1. So here's what I'm doing
RewriteRule ^/article/([0-9]+)\.php /article.php?id=$1
All I get with this is
The requested URL /article/.php was not found on this server.
I think this has to do with the rest of my rewriting. Here's all the code I got
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^/article/([0-9]+)\.php /article.php?id=$1
Any ideas?
Have you tried something like:
RewriteRule ^article/([0-9]+)$ article.php?id=$1
Instead of what you have?
EDIT:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([0-9]+)$ article.php?id=$1
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Not Found when shortening URL

So I have shortened article.php?id=10 to article/10. And it all seemed to work fine. But little did I know that it ruined the rest of my URLs. So with http://localhost/forgot/, I'd have to go to http://localhost/forgot/index to actually reach it. Here's what I'm using
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([0-9]+)$ article.php?id=$1
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I want to go to http://localhost/forgot/ instead of http://localhost/forgot/index/ Any ideas?
Have your rules like this:
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([0-9]+)$ article.php?id=$1 [L,QSA,NC]
# rewrite from /dir/file/ to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

Rewrite-rules issues : .htaccess

I want to make this URL clean:
news.php?post=13
but when I tried to use this RewriteRule:
RewriteRule ^(news)\/([0-9]+)\/?$ news.php?post=$2
It only shows the news page, no post content.
It has been bugging me for the past 2 days, and I can`t figure it out. Can someone help me with this?
Here's the entire Htaccess
RewriteEngine on
RewriteRule ^(news)\/([0-9]+)\/?$ news.php?post=$1
RewriteRule ^(verify)/(.*)/([a-zA-Z0-9_-]+)$ validate_account.php?usr=$2&hash=$3 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(news)\/([0-9]+)\/?$ news.php?post=$2
use $1
Try this config:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^news/([0-9]+)\/?$ news.php?post=$1
RewriteRule ^verify/(.*)/([a-zA-Z0-9_-]+)$ validate_account.php?usr=$1&hash=$2 [QSA,L]
RewriteRule ^(.*)$ $1.php
Try this method:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^news/([0-9]+)\/?$ news.php?post=$1

Changing a htaccess rewrite rule to this?

I use this rules
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ /used-details.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
to rerwrite all the www.domain.com/1234 to www.domain.com/used-details.php?id=1234
and it works correctly.
How can have the www.domain.com/used-details/1234 instead of the www.domain.com/1234
Thank you
Use this
RewriteEngine on
RewriteRule used-details/(.*)/ used-details.php?id=$1
Try this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^used-details/([0-9]+)/?$ /used-details.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
It will change www.domain.com/used-details/1234 to www.domain.com/used-details.php?id=1234

How to change URL's in .htaccess

I know this topic already been here, but I need help because I cant find an answer.
Here is my problem:
I am trying to change urls
example.com.au/admin/pages.php
to
example.com.au/admin/pages (without .php extention)
then I am trying to:
example.com.au/admin/edit.php?id=1
to
example.com.au/admin/edit/1
I have this .htaccess, please helkp me sort this problem. thanks
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php
RewriteRule ^edit/([^/.]+)/?$ edit.php?id=$1
Try this :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^edit/(.*)/?$ edit.php?id=$1
RewriteRule ^(.*)$ $1.php
Try this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
THIS IS WORKING, thanks everyone got my answer my self.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^admin/pages/edit/(.+)$ admin/pages/edit.php?id=$1 [L]

Categories