How to remove .php and get var after slash on .htaccess - php

Good day. I need to remove .php at the end of a file and also get the last /var into a php $_GET var from .Htaccess.
I want this url rewrite:
http://example.com/file/123
into
http://example.com/file.php?id=123
But this must work if file is index.php like this:
http://example.com/123
into
http://example.com/index.php?id=123
Thanks for all your help :)

You can use these rules in site root .htaccess:
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)/?$ $1.php [L]
RewriteRule ^([\w-]+)/?$ index.php?id=$1 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?id=$2 [L,QSA]

This should meet your needs
RewriteEngine On
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([0-9]+) http://example.com/file.php?id=$1 [NC]
This is a great article on the subject http://corz.org/server/tricks/htaccess2.php

Use this in .htaccess for remove the extensions :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
And if you need a new extension add as many lines you want
RewriteRule ^([^.]+)$ $1.html [NC,L]
For more info read : https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/

Related

Change url from profile.php?user=myUsername to profile/myUsername with .htaccess

I am using xamp to host my webpage on my PC and I want to change this url from
http://localhost/loginsystem/profile.php?user=myUsername
to
http://localhost/loginsystem/profile/myUsername
I have tried the following and doesn't work and in return gives server error 500.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/profile/([a-zA-Z0-9]+) /profile.php?user=$1 [NC, L]
and
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/profile/(.*)?$ /profile.php?user=$1 [NC, L]
Any help would be great still pretty new. Thanks in advance!
This should do the trick
RewriteRule ^profile/([^/]+)/?$ profile.php?user=$1 [L]
Also make sure that your .htaccess file is in the loginsystem folder. Otherwise you will need to put this in your rewrite rule
RewriteRule ^loginsystem/profile/([^/]+)/?$ profile.php?user=$1 [L]
Replace .htaccess code with:
Options +FollowSymLinks
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^profile/([a-zA-Z0-9]+)/$ profile.php?user=$1 [L]
Now you can get user query with GET request.

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]

How to remove .php extension only for pages under a sub directory?

I am trying to remove .php extension for only pages under a subcategory folder of the website.
currently: example.com/blog/my-first-blog.php
what i want: example.com/blog/my-first-blog/
I have tried the following rule in .htaccess (placed in root) but it still shows .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/blog/$1.php -f
RewriteRule ^(.+?)/?$ /blog/$1.php [L]
The best way to achieve result you want is building a routing.
Firstable, you need to rewrite all traffic to your index.php:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L,QSA]
Then, you need to write router that will be parsing URL paths.
The simplest example of router is:
$url = urldecode(preg_replace('/\\?(.*)$/', '', $_SERVER['REQUEST_URI']));
if ($url == '/contact/about') {
include 'contact.php';
}
You can check how does it work in PHP frameworks to get a better solution, or use one of them.
I figured it out
I just had to create .htaccess file under the "/guide/" directory. with the following rule note RewriteBase /guide/ for making sure it only removes .php under "/blog/" folder
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Try this --
RewriteEngine on
RewriteBase /
RewriteCond %{http://www.example.in/} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.php $ http://www.example.in/$1 [R=301,L]
Try this
DirectorySlash Off
Options -MultiViews -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

url rewrite issue by .htaccess

I have issue on url rewrite i am want to change url
http://www.example.com/mbl.php?domain=example.com to http://www.example.com/mbl/example.com
So i have written code myself by as below
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^/mbl/([^/]+)$ /mbl.php?domain=$1
But its not working. so please to my error.
Thanks
You should try your rules this way and remove the leading slash from the rewriterule with mbl since you are using it in .htaccess file.
Give these rules a try.
#Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^mbl/([^/]+)$ /mbl.php?domain=$1 [L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Remove file extensions from my Php files

How can I remove .php extension from php files
htacess file
RewriteEngine On
RewriteCond
%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
any help is much appriciated, Thanks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ $1.php [NC,L]
If you go to example.com/test/ it will load example.com/test.php
For hat you should define an entry point.
Then you can rewrite your URLs to a parameter of a specified file in that case the index.php.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ index.php?url=$1 [NC,L]
Then you can filter that parameter in your script and show the correct content.
You have an URL something like:
www.example.de/test-test
Then everything is rewritten to your index.php. Other possibility if you rewrite to an existing file. You can do something like this:
RewriteRule ^(.*)/?$ $1.php [NC,L]
Then you fetch everything behind the slash and call an existing PHP file.
Remove .php extension with .htaccess

Categories