3

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.

Chibuzo
  • 6,112
  • 3
  • 29
  • 51

4 Answers4

4

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])

David Bélanger
  • 7,400
  • 4
  • 37
  • 55
3

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]
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

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/

Alex
  • 7,538
  • 23
  • 84
  • 152
1

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>
Electronick
  • 1,122
  • 8
  • 15