3

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.

4 Answers 4

1

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]
Sign up to request clarification or add additional context in comments.

Comments

0

For hidding ?val you should use POST instead of GET. And take a look on this article for .htaccess he has defined good in here.

4 Comments

But i am creating dynamic pages where GET is working cant use post
You can use POST in dynamic too.
yes i can but there are lot of pages and also use of gateways where i have used header redirection so i cant change that now
Yes that is the problem ;)
0

Regarding hiding the query string. When the php page runs it can first check for a query string. If one is found you can pull the data out of the query string, store it somewhere (session probably) and redirect to the page with no query string (and no file extension for that matter).

However if you do this you're obscuring most of the benefits of using a query string.

3 Comments

This means i cant hide query for whatever the page is by .htaccess ??
I'm sorry, I wasn't clear. I edited my answer. You can't remove the query string in the server, then it won't be available to the script and you'll loose that data. But you can let the script hide it.
here you can stackoverflow.com/questions/21892542/… but we have to specify each page there . I want whatever the page is should automatically hide query strings
0

To remove the php extension completly, you can use :

 RewriteEngine on

#1redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ %1 [NC,L,R]

#if the request is not for a dir
RewriteCond %{REQUEST_FILENAME} !-d

 #and the request is an existing php file
 RewriteCond %{REQUEST_FILENAME} !-f
 #then rewrite the request to orignal php file
 RewriteRule ^([^/]+)/?$ $1.php [NC,L]

4 Comments

Hiding .php extension is resolved but hiding query string is not resolving @Starkeen
@sharefun do u want to discard the query strings from urls?
No variable should be accessed to whatever functionality they are made for but it should not seen on url . Its functionality should be their
@Sharefun its not possible. You need to rewrite your variable , forexample ?var=foo to /foo , so that variable can be internally rewritten.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.