Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide .php extention from urls with query string

I am using .htaccess file for url rewriting . I've written all pages with .php extention and to hide them i am using this code

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]`

But this hides when i write "www.example.com/abc.php" to "www.example.com/abc" . I want them to change automatically like when i click on a link

<a href="example.php">example</a>

then page link should open like "www.example.com/example" not "www.example.com/example.php"

Also how to hide "?var="hello" from all pages but variable should be sent to that page is that possible ?.

UPDATE

For example here i am using this

<?php 
if(isset($_GET['hi'])){
echo $_GET['hi'];
}
?>
<a href="a.php?hi=sdsdsd">a</a><br>
<a href="b.php">b</a><br>
<a href="c.php">c</a><br>
<a href="d.php">d</a><br>

I want what ever the page is should be accessed without extension and query string should be hided but must be accessed.

like image 312
Share fun Avatar asked Jul 07 '26 09:07

Share fun


1 Answers

Use this .htaccess:

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
like image 85
Waqas Shahid Avatar answered Jul 10 '26 00:07

Waqas Shahid