Oswaldo C.
Oswaldo C.

Reputation: 99

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

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 :)

Upvotes: 1

Views: 223

Answers (3)

anubhava
anubhava

Reputation: 785276

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]

Upvotes: 1

Brucea
Brucea

Reputation: 23

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/

Upvotes: 0

Jez Emery
Jez Emery

Reputation: 686

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

Upvotes: 0

Related Questions