Remove file extention from urls - php

I have some urls in my website like this..
http://www.mydomain.com/about.php
http://www.mydomain.com/contact.php
http://www.mydomain.com/signup.php
http://www.mydomain.com/testimonials.php ...... and much more
Now I am trying to convert above urls to user friendly url
http://www.mydomain.com/about
http://www.mydomain.com/contact
http://www.mydomain.com/signup
http://www.mydomain.com/testimonials
This is code in my .htaccess file that I am tried so far. But it doesn't work.
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
Anyone can help me to remove .php etension from my urls??
Thank you.

Get rid of the conditions you are using and instead use the below 2 lines in your htaccess file
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^whatever/?$ whatever.php [NC]
This will work even if user types a forward slash after the name or not
If you want to make them dynamic, here's a good tutorial on that

for a proper implementation so as not cause confusion during deployment if you dont have access to the htaccess file of your server or if it is override. You can consider a library like toro-php http://toroweb.org/ for easy routing.

Related

How rewrite TWO URLS for .HTACCESS?

One pages of site have such adds: domain.com/posts/name.php
Other pages of site have such adds: domain.com/pages/name.php
I need to cut from these adds posts/ and pages/ only.
Firstly, I tried to use in .htaccess next rules:
RewriteRule ^([A-z0-9-]+)$ /posts/$1
RewriteRule ^([A-z0-9-]+)$ /pages/$1
Don't help.
Secondly, I tried to use in .htaccess such rules and conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ posts/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ pages/$1 [L]
As result: image of mistake 404
Also I tried to use such method:
RewriteCond %{REQUEST_URI} ^([A-z0-9-]+)$/(posts|pages)/([A-z0-9-]+)/$1 [NC]
RewriteRule .* domain.com/%1%3
Don't help....
what i am understanding you need to remove the post and pages from the url following code will help you for that.
You would need the rewrite module of Apache: mod_rewrite.
Then do something like this:
RewriteEngine on
RewriteRule ^post/(.*)$ $1
RewriteRule ^pages/(.*)$ $1
Here is the official documentation of mod_rewrite: click
please test it onece I dint test the code but did same for replacing url and its work for me.
Thanks.
The solution of this problem such:
We enter all information about the page in the MYSQL database:
image of database
We generate a page from the database:
generation of page (php)
Set the generated page for Human-Friendly Url:
file .htaccess
So everything works, and POSTS there are in their folder in the database, and PAGES in their folder.
folder in database
All is clean. If you can do better somewhere, then correct me, please.

htaccess url rewriting, ignoring files

I am trying to come up with a rewrite rule, but I am having problems.
What I need - any url that starts with /services/XXX to be redirected to /services/api.php?service=XXX
I also want to ignore any files or folders that might also match.
What I have so far:
RewriteRule ^services/([a-z]+)$ /services/api.php?service=$1 [NC, L]
But this does not work at all, it shows a 404 page saying that file is missing when I test it.
Any help is much appreciated.
To ignore the files that exists just ignore the "RewriteCond"s.
Just rewrite everything that comes into services/ to api.php?service ...
Put this into your services folder.
Be careful with your encoding, save the file as .htaccess with a encode that your server reads.
# .htaccess mod_rewrite
RewriteEngine On
RewriteRule ^(.*)$ api.php?service=$1 [QSA,L]
If you don't need to avoid Files, then just go to:
# .htaccess mod_rewrite
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ api.php?service=$1 [QSA,L]

Redirecting existing URLs to SEO Friendly URLs

I have came accross a problem that every .htaccess query I've tried wasn't worked out ! I have URLs like this:
http://www.example.com/index.php?x=product
And I want to change it to a user friendly URL like this:
http://www.example.com/product/
Or it can be:
http://www.example.com/product.php
I've tried this code below:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^x=product$
RewriteRule ^index.php.*$ http://www.example.com/product.php? [R=302,L]
Now, it is redirecting perfectly, but this is not the problem. I've used this for only SEO so I must include http://www.example.com/index.php?x=product in the product.php file. Any help can be precious, thanks...
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?x=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ index.php?x=$1 [L,QSA]
This will redirect /index.php?x=product to /product/ and will rewrite it internally to /index.php?x=product in 2nd rule.
You don't need to put anything in the product.php file. Make sure there is a .htaccess in the directory that has the files you want to make url/seo friendly.
To have it SEO friendly make sure its in this format
http://www.example.com/product/ not http://www.example.com/product.php
if you must have a file extension, have it in http://www.example.com/products.html (you want to tell the search engine the page isn't dynamic although it is to get better pagerank)
Insert this in your .htaccess
RewriteRule /(.*)/$ products.php?x=$1
the (.*) pulls the elements out and puts them in variables $1

why i cant use htaccess in xampp?

my name is mr.rico , i just build up my website for a anime call akifansubs.com.
my design and the program is not have any problem.... but....
where i try to use .htaccess file my xampp does'nt respons
ex
akifansubs/index.php become like this akifansubs/index.
and this the code for htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: php, htm, asp
this is what i found a few hours ago
what should i do ? try to u make some change in xampp httpd con but it doesnt work :(
I know that this question was asked multiple times already and is answered, but I will give a little more comprehensive answer based on my experience.
Here is the .htaccess code snippet that will help you:
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# End of Apache Rewrite Rules
I want to stress some important things here for everybody's reference:
This code snippet doesn't remove entry scripts from url (such as index.php used by many PHP frameworks)
It only removes .php extension, if you want to remove other extension as well (e.g. .html), copy and paste 3rd block and replace php with other extension.
Don't forget to also remove extension from anchors (links) href.

Rewriting in .htaccess with forward slashes

here is what I need. I have the url pearlsquirrel.com/profilecomments.php?u=eggo.
eggo being my username and the part of the dynamic url that changes. I would like to rewrite the url to say pearlsquirrel.com/eggo/comments, using .htaccess.
Here is what I have so far:
RewriteRule ^(.*)$ $1.php
RewriteRule ^([a-zA-Z0-9_-]+)$ profilecomments.php?u=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profilecomments.php?u=$1
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel\.com$ [NC]
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1/comments [L,R=301]
but I just can not get it to work. Any help would be greatly appreciated. Thanks!
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/comments$ profilecomments.php?u=$1 [L]
NOTE
If you've used relative paths for your images, stylesheets etc. you need to change those into absolute paths or relative paths using the servers root folder as base in order to get your site to display properly.
For example, it will think that images/image.png is /eggo/comments/images/image.png
But, if you instead add a preceding slash /images/image.png your file paths will always start from the servers root folder and your site won't get messed up when you're rewriting your URL's.
Your first rule, overrides all the rest.
What you are describing (if I understood correctly), you need to handle /user/comments by profilecomments.php?u=user
RewriteRule ^([a-zA-Z0-9_-]+)/comments profilecomments.php?u=$1 [L]
this should do it.

Categories