I have a url like this - www.example.com/blog.php/username, it will take you to the username's blog page, but I want to use a url like this instead - www.example.com/blog/username to get to the same file (blog.php). Please what are the steps I need to take to achieve this? Thanks for any help.
-
you using any framework? – Alex Mar 26 '12 at 13:29
-
What's wrong with the site search? – mario Mar 26 '12 at 22:00
-
possible duplicate of [Redirect using username in URL with .htaccess](http://stackoverflow.com/questions/6861469/redirect-using-username-in-url-with-htaccess) – mario Mar 26 '12 at 22:02
4 Answers
Create a file in your root direstory named .htaccess and past this in :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^blog/(.*)$ yourpage.php?username=$1 [L]
</IfModule>
(.*) will take any caracters
If you want to use only letters and numbers change (.*)
by ([A-Za-z0-9])

- 7,400
- 4
- 37
- 55
Enable mod_rewrite and .htacess. Then add this code in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# 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
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
# finally this is your rewrite rule
RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC]

- 761,203
- 64
- 569
- 643
Removing file extension:
There are two ways to do this. The easy way which just looks at a request, if the requested filename doesn't exist, then it looks for the filename with a .php (or .asp or whatever) extension. In an .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
Now if you go to: http://domain/about the server will interpret it as if you went to http://domain/about.php.
Makes sense, but if we're already breaking the relation between URL and filename, we may as well break it intelligently. Change that .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Source: Dave Dash -> http://davedash.com/2006/07/26/how-to-remove-file-extensions-from-urls/

- 7,538
- 23
- 84
- 152
1 You should install mod-rewrite
2 You should make sure that for your doc root directory is "AllowOverride All". Ordinary after virtualhost configuration you should find something like:
<Directory "/your/doc/root/path/">
AllowOverride All
</Directory>
3 After that you should start rewrite engine and create rewrite rule in your .htaccess as David Bélanger advises:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^blog/(.*)$ blog.php/$1 [L]
</IfModule>

- 1,122
- 8
- 15