Remove file extension on url - php

Good day!
Got a lot of questions here but i guess i can't ask it all at once so i'll start with the problem i am facing now.
So, I am new to .htaccess and I don't know how or if it is possible to remove the file extensions on url, like for example I am accessing my home page in this url "www.sniper.com/home.php", so the question is clear. Is it really possible to use .htaccess to remove the file extension so that when you are at homepage the url will look just like "www.sniper.com/home" ?

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

Related

htaccess file to make the clean url

I am using wamp server which consists of my project folder testCase which consists of few php files
testCase
- view.php
- add.php
- delete.php
- .htaccess
In the .htaccess file I have code like
RewriteEngine on
RewriteRule ^view?$ view.php
I am trying to run my project with url as "localhost/testCase/view" rather than "localhost/testCase/view.php" which results as Not found
I have found many tutorials regarding this htaccess in stackoverflow. Since I am a very beginner to programming, I could not fix this issue. Can anybody please help me. Even I get down vote or duplicate question I am eager to fix my issue.
Thank you.
You want to remove .php extension,
Try below rule,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)$ $1.php [QSA,NC,L]
Above will work for all your php files in testcase directory.

Removing page extension with .htaccess bugs Facebook Open Graph

I researched for removing page extensions (Ex: /page.php to just /page) and I found this article: http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
So, I inputed this code into my .htaccess file. It really worked, but when I paste/type my index link at Facebook status with no trailing slash at the end, the Open Graph kinda bugs. There's no thumbnail, no right title and description.
Check it: http://www.aftercolors.com.br
Would the solution be remove the code from .htaccess and create subfolders with index files to have the pages without extension?
That's the code of my .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You can try this one for just single link:
RewriteRule ^page page.php [L]
If you want just to redirect that one simple file. It's better to use id's to get bigger spectrum.

Url rewrite to takeout

Good days guys, I need your help with this url rewrite.
The are two rewrites I want to do.
1) I want to take out .php from the url even though my files is saved as .php
2) I want to rewrite the url below
www.example/account/product.php?Sef=shoes&pid=3
To something like this
www.example/account/product.php?Sef=shoes/pid/3
If you can help me. It will be lovely. Thanks
For your second problem you don't need htaccess for that just parse $_GET['Sef'] on your server side.
As per php extension try this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

How to create a directory with a single file in it?

I'll be signing businesses up to advertise on my website, and I want them to have a direct URL for their customers to go to.
Like, instead of www.website.com/page.php?id=324234234,
I want to have www.website.com/businessname
Is there a simple way to do this? I've searched and seen a whole bunch of different things people are trying to do but I haven't seen anything that's the same as what I want to do.
I'm using a VPS, and I want to make sure that I don't open up permissions so that anyone can get in there and mess things up.
Also, these users will not be signing themselves up. I will be doing that.
The simplest way to get my end result is what I'm looking for. Thanks!
Basic URL rewriting could work.
Add to your .htaccess file
RewriteEngine on
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
Then use PHP to rewrite the businessname to the ID of the company / find the data.
Of course .htaccess rewrite rules is a complete science if you need more complex rewriting...
Re-iterating what jtheman said with a little more explanation:
Create a file named .htaccess with the contents:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
You need, of course, the ability to have directory level .htaccess enabled - you're using a VPS so you should be able to do this if it is not already enabled.
So let me explain what each line will do.
RewriteEngine on
Turns on the ability to URL re-write
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Tells Apache not to re-direct files that exist in the directory already
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
This is where the magic happens.
^(.*)$ this part is like a regular expression match. It will tell Apache to collect any URLs that have any characters within them and redirect them to page.php?businessname=(.*)
So, if you post:
www.website.com/stackover
It will really be sending: www.website.com/page.php?businessname=stackover
Then you can just use $_GET[businessname] to dynamically update the page.
Hope this helps!

URL Routing method for core php

I have web site in core php and I want to make my SEF URL.
In my web site for transferring control from one page to other i have used something like
header("Location: edit.php?id=12");
I read one article but couldn't get it how to implement it.
Link of article
So how i can make url to read like "mysite.com/usr/edit"
I read lot on google i got the idea that i need to do something in .htaccess but i dont know what needs to do with url in php so i dont have to change major code in site.
Thanks in advance.
You can do it with the .htaccess code given below:
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
after creating .htaccess file with above code. You will have all the stuff after the file name ex. "/usr/edit" in the GET parameter $GET['url']. After that you can do anything.
Here is the working example of my MVC framework: https://bitbucket.org/ManthanB/my-framework/
You can contact me for further information.

Categories