.htaccess remove extension while preserving uri parameters - php

i was trying to rewrite my urls in a friendly way; i have written all the php code to handle everything.
The only thing that i am unable do is removing the .php extension while preserving the uri parameter.
So, essentially, i'm looking for some .htaccess rules to change from this:
www.example.com/biography.php/john-doe
to this:
www.example.com/biography/john-doe
(john-doe isn't a real file, it's only the GET parameter passed to biography.php)
Thanks everyone.

If the links on your site are already like this
www.example.com/biography/john-doe
and you want to process them on the server like this
www.example.com/biography.php/john-doe
add the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
RewriteRule ^([-a-zA-Z]+)/([-a-zA-Z]+)$ $1.php/$2 [L]

Should do:
RewriteRule ([^\.]).php(.*) $1$2 [QSA,R=301]

Related

How To Rewrite URL in .htaccess?

I have a site/blog that pages URL as www.example.com/sample-page and I want to make it like www.example.com/p/sample-page.html For this purpose I am thinking to use .htaccess file to write rewrite/redirect rule to do it. So can we make it using .htaccess?
You probably should be looking at the mod_rewrite documentation for apache (the rules in there can be placed into a .htaccess file).
http://httpd.apache.org/docs/current/rewrite/remapping.html
However, for your specific case (which I think is to end up mapping the real URL www.example.com/WP/p/sample-page.html to www.example.com/WP/sample-page), you could do something like this:
RewriteEngine on
RewriteRule ^WP/sample-page$ WP/p/sample-page.html [NC]

Rewriting a URL to a PHP parameter from inside a subdirectory

What I'm asking for is simply rewriting any URL-given sub-directiories to a PHP URL parameter for nicer looking, user friendly URL.
I know that this is possible using the Apache Rewrite Engine and associated parameters.
The problem on my end is that I do not want to rewrite i.e
mydomain.com/parameter ----------->
mydomain.com/index.php?id=parameter
But rewriting
mydomain.com/subdirectory/parameter ----------->
mydomain.com/subdirectory/index.php?id=parameter
In my root folder I have an .htaccess file for 404 errors and such, redirecting non-existant pages to the main one. During the tests I've done I have deactivated these thinking that they could overwrite the other .htaccess file.
I tried...
RewriteEngine On
RewriteRule ^directory/([^/\.]+)/?$ index.php?id=$1 [L]
...and other snippets similar to that one.
The .htaccess file containing this code was placed in the root/directory folder.
What I wanted to achieve with that was to redirect root/directory/string to root/directory/index.php?id=string.
Since my knowledge of .htaccess and rewriting is obviously limited, I need answers to two questions.
Will a 404 rewriter in the root folder overwrite any other rewriter in a subdirectory?
How do I achieve what I'm after? (much like how url-shorteners work - bit.ly/shortened) from a subdirectory?
This rule should work for you in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^(directory)/([^/.]+)/?$ /$1/index.php?id=$2 [L,NC,QSA]
Make sure there is no other .htaccess file in directory.

How to have a PHP file be used for multiple URLs

I want to have a single PHP file that takes care of multiple URLs in a subdirectory.
For example, my site is http://www.startingtofeelit.com/. I want one php file, say, called playlist.php which would handle when a user goes to http://www.startingtofeelit.com/playlist/101 or if they go to http://www.startingtofeelit.com/playlist/142 etc. I want to be able to strip the number (101, 142 in my example urls above) to use as a variable (the playlist ID), so I can display the correct playlist.
I know that I can create an index.php in my playlist subdirectory and use GET variables like http://www.startingtofeelit.com/playlist?id=102 and get the ID that way, but this is much sloppier looking and I'd like to be able to know how to do it the other way.
My site is built on WordPress, but I don't think this should make a difference in any way.
Well, you cannot achieve this with PHP alone.
If you use Apache, you can use .htaccess
If you use IIS, you can use URL Rewrite
The basic idea behind those modules is to mapping from one URL to another URL. For example: you would want to map from
http://www.startingtofeelit.com/playlist/142 =>
http://www.startingtofeelit.com/playlist.php?id=142
You can express the URL mapping in regular expression. For example, in .htaccess (Apache). You can write like this
RewriteRule ^playlist/([0-9]+)/?$ playlist.php?id=$1
Noted that, you need to have .htaccess file in your website directory. Since, you are using Wordpress, chance that you have existed .htaccess is high. You can simply append that line of code to existed .htaccess
The following is an explanation of the regular expression:
^playlist/ # any URL start with playlist/
([0+9]+) # following by number, and store it as $1
/?$ # end with or without /
Mapping to
playlist.php?id=$1 # where $1 is taken from the matched number from our pattern.
This is usually handled in a way similar to what you already tried. However, it's common to use a re-writing script so that your application will accept a clean URL such as:
http://www.startingtofeelit.com/playlist/142
...and re-write it for your application as such:
http://www.startingtofeelit.com/playlist?id=142
For example, if you're using an Apache web server and have the mod_rewrite module installed and enabled, you can use the following snippet in an .htaccess file and use your GET parameter as you indicated you already know how to do. Other popular web servers have unique URL re-writing modules that will let you do the same.
<IfModule mod_rewrite.c>
RewriteEngine On
# Rewrite this:
# http://www.example.com/somepage/1
# ...into this:
# http://www.example.com/somepage?id=1
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Htaccess redirect all .html pages to php get variables

My urls are currently like:
/events.html
/contact.html
What I want to do is, redirect them to a php get variable.
E.g events.html = index.php?page=events
Any ideas on how this could be accomplished?
Thanks A lot.
You are looking for mod_rewrite here.
With the rule something along the lines of (assuming mod_rewrite is installed and enabled and you're putting it in a .htaccess file)
RewriteEngine on
RewriteRule ^/(.*).html /index.php?page=$1

PHP: Hide file extension from URL using .htaccess

I want to hide file extensions from a URL like
if the current URL is
http://localhost/salsgiver/administrator/menus.php?sect=about
then the new one will be exactly
http://localhost/salsgiver/administrator/menus/sect/about
and so on, similary if the current URL is
http://localhost/salsgiver/administrator/products.php?id=1
then the new one will be exactly
http://localhost/salsgiver/administrator/products/1
Or some thing different, so that the viewer could not guess the exact URL.
I searched Google and found some matter on
http://roshanbh.com.np/2008/02/hide-php-url-rewriting-htaccess.html
and also used, but it does not work and the mod_rewrite module is also enabled in Apache. And when I create the .htaccess file to secure a folder from all using
deny from all
it works fine.
You could do this, but mod_rewrite is much easier to use if you use a single index.php which then chooses which file to open (products.php or menus.php)
For a single index file:
RewriteRule ^(.*)$ index.php?query=$1 [L]
For multiple files:
RewriteRule ^(.*?)/(.*?)/(.*?)$ $1.php?$2=$3 [L]

Categories