Hide .php extention from urls with query string - php

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
example
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<br>
b<br>
c<br>
d<br>
I want what ever the page is should be accessed without extension and query string should be hided but must be accessed.

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]

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.

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.

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]

Related

Make different types of URLS pretty with htaccess

I have two types of urls I want to make readable/pretty:
www.mydomain.com/index.php -> www.mydomain.com/index
(remove .php)
And
www.mydomain.com/information.php?=story1 -> www.mydomain.com/information/story1
(replace .php=? with /)
Im using $_SERVER[REQUEST_URI] to grab the "story" from the database
I've tried for days and hours, but can't find any solutions. Any suggestions?
Okay, I figured it out myself.
First:
www.mydomain.com/index.php -> www.mydomain.com/index
(remove .php)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Second:
I had to make a few changes, but heres goes:
Goal:
www.mydomain.com/information.php?story=1 -> www.mydomain.com/information/1
RewriteEngine on
RewriteRule ^information/([A-Za-z0-9-\s]+)$ information.php?story=$1
And this is added to the php code:
$story_str = $_GET['story'];
$story_str = 1
In the tag of your php code, add this to make sure your images and CSS is loaded:
<base href="/" />
And finally, add this to your .htaccess also to make sure your images and CSS is loaded:
RewriteRule ^detail/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
You should try this:
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]

Hiding true URL from website visitor

I have a php website. The various pages are distributed in various directories. Now, when I try to send the link to any page to a user, I have to put some URLs like "www.mydomain.com/collection/latest/requestpage.html" or "www.mydomain.com/api/get/someapi.php". But this URL is too complicated. I want the pages to be referred via the URLs like "www.mydomain.com/collection/requestpage" and "www.mydomain.com/someapi" and also this same address to be displayed in the url bar of browser.
Thus, loading "www.mydomain.com/collection/requestpage" takes the user to "www.mydomain.com/collection/latest/requestpage.html" and loading "www.mydomain.com/someapi" takes the user to "www.mydomain.com/api/get/someapi.php".
So,
1) I want the actual directory structure to be hidden from the URL,
2) I want to hide the file type extension of the page being loaded, and
3) Show this modified URL in the address bar of the browser when accessing the specified page.
I know this can be done using a .htaccess file but I have never written anything like this before. I have a .htaccess file on my directory that redirects any request coming to "mydomain.com" to "www.mydomain.com". I want to add some code to this htaccess file to do the tasks I mentioned. Please suggest what requires to be done.
Thanks in advance.
1) to remove the directories from the URL just add the following codes to your .htaccess file
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^FOLDER_NAME/ FOLDER_NAME%{REQUEST_URI} [L,NC]
Change FOLDER_NAME with your actual folder names
2) to remove extensions from the URL use following code
RewriteEngine on
RewriteBase /
for html
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
for php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
EDIT: while working with POST requests you have to change the RewriteRule as following
RewriteRule ^ %1 [R=307,L]
3) You are done with what you wanted
Hope this helps.

.htaccess to hide php extension preventing POST

I've been trying to hide the php extensions for my site. And I've been able to pull together a few Stackoverflow answers to do just that. However, I just have one last issue where when I POST, the resulting page is showing the .php extension. Can anybody help me on what I need to tweak with my htaccess file to not show .php completely?
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
Here are the two resources I used from Stackoverflow:
How to hide .php extension in .htaccess
Website Form do not carry any value when .htaccess File Extension Remover Code is Used
Remove this:
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
It says:
Dear mod-rewrite
If the request method is POST, whatever the url is, would you please leave it
unchanged without doing any rewriting? And would you please not apply any other rules?
Sincerely yours,
etc. etc.

Remove File Extension and URL Variable using .htaccess

I'm making up myself a small blog and I found a useful .htaccess file to remove file extensions:
AddType text/x-component .htc
RewriteEngine On
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php
This works just fine and all pages are showing up .php less. I know wanted to extend this so when I click a link to a specific blog post (say /blog/index.php?art=1) it just shows in the url as website/blog/1. I thought to tag on to the end of the .htaccess file:
RewriteRule ^blog/(.*)$ blog/index.php?art=$0 [L]
But that doesn't seem to be working. EDIT Actually it breaks the blog page so no snippets are pulled through from the DB
My .htaccess file is in the root directory and the blog files are /root/blog/index.php
Any help would be gratefully appreciated
Unlike most other languages, the parameters in .htaccess are not 0-based. To access the first parameter, you should use $1, not $0.
The following should work:
RewriteRule ^blog/(.*)$ blog/index.php?art=$1 [L]
It might also be worthwhile to add some tests in there, for example you might only want numerical values passed to art, so you can improve it using:
RewriteRule ^blog/([0-9]+)$ blog/index.php?art=$1 [L]
Also, it might be worthwhile to add the QSA flag, since this will also preserve any query string that is passed in the original URL:
RewriteRule ^blog/([0-9]+)$ blog/index.php?art=$1 [L,QSA]

htaccess add .html extension for urls with or without trailing slash

So to begin with I have a custom url rewrite that sends a request variable to a php script
Rewrite rule is below:
RewriteRule ^([\w\/-]+)(\?.*)?$ test/index.php?slug=$1 [L,T=application/x-httpd-php]
So if you access something like domain.com/slug-text it sends slug-text to index.php located in folder named test.
What I want is all my urls to look like domain.com/slug-text.html, but slug-test variable should still be sent to index.php file.
And
What I can't figure out is the redirect. I want all the old urls to be redirected from domain.com/slug-text or domain.com/slug-text/ to domain.com/slug-text.html and slug-text sent to index.php file located in test folder.
Searched a lot but could not find the answer for this question anywhere on the Internet.
Thank you all for the help.
UPDATE:
my new code is:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/\-]+)?[\w-])(?!:\.html)$ http://domain.com/$1\.html [L,R=301]
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
domain.com/slug-text/ does not get redirected to domain.com/slug-text.html
domain.com/slug-text works as intended redirecting to domain.com/slug-text.html
What do i need to change?
This rule:
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
Will trap domain.com/slug-text, domain.com/slug-text/ and domain.com/slug-text.html and send slug-text to /test/index.php inside slug param.
If you really want to redirect using [R=301] from old urls to new then use this:
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
Also note that as using explicit redirect bottom rule is modified to trap url's ending with .html
It is also advisable (if your .htaccess does not already contain this) to filter conditions for existing files and folders not to be trapped by your redirect rules. Simply add these lines before RewriteRule lines:
# existing file
RewriteCond %{SCRIPT_FILENAME} !-f
# existing folder
RewriteCond %{SCRIPT_FILENAME} !-d
And if using symlinks:
# enable symlinks
Options +FollowSymLinks
# existing symlink
RewriteCond %{SCRIPT_FILENAME} !-l
// addition
Your .htaccess file should look like this:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
This is supposed to redirect /slug-text to /slug-text.html
RedirectMatch ^/([\w-]+)/?$ http://domein.com/$1.html
This is in the case when slug-text is only letters, digits, – and _.
Rewrite slug-text.html to a php file and pass the slug as a param:
RewriteRule ^([\w-]+)\.html$ test/index.php?slug=$1 [R,L]
If you have both line in your .htaccess the first one will do the redirects from the legacy URLs to the new ones and the second one will process the request.

Categories