How to Create Friendly Urls With .Htaccess

How can I create friendly URLs with .htaccess?

In the document root for http://website.com/ I'd put an htaccess file like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Then in your PHP script you can manipulate the $_GET['url'] variable as you please:

$path_components = explode('/', $_GET['url']);
$ctrl=$path_components[0];
$id=$path_components[1];
$tab=$path_components[2];

Creating user friendly URLs with .htaccess file

Create a directory posts with an index-file (present a list of posts, link to the pretty url) and this .htaccess:

RewriteCond %{REQUEST_URI} ^/posts/[^/]+/.*$
RewriteRule ^/posts/([^/]+)/$ postdetails.php?slug=$1 [QSA,L]

The pretty url

in this pattern:
http://www.example.com/posts/slug/

e.g.:
http://www.example.com/posts/keywords-are-here/

In the file postdetails.php you can evaluate the param $_GET['slug'].

Creating user friendly URL through .htaccess

With your shown samples, please try following. Please make sure you clear your browser cache before testing your URLs.

<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteBase user_notes/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ index.php?id=$1
</IfModule>

How to write htaccess rewrite rule for seo friendly url

First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.

Then, put this code in your htaccess (which has to be in root folder)

Options -MultiViews
RewriteEngine On

# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /section/%1? [R=301,L]

# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]

How to create htaccess seo friendly url rewriterules for e.g a/b/c

These 2 rules should be able to handle your pretty URL schemes:

RewriteRule ^pages/([\w-]+)/?$ page.php?pages=$1 [QSA,NC,L]

RewriteRule ^pages/([\w-]+)/([\w-]+)/?$ page.php?pages=$1&title=$2 [QSA,NC,L]

Pretty URLs with .htaccess

Thanks for the idea @denoise and @mogosselin. Also with @stslavik for pointing out some of the drawback of my code example.

Here's how I do it:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^user/([0-9]*)/([a-z]*)$ ./index.php?user=$1&action=$2
RewriteRule ^user/([a-z]*)$ ./index.php?user&action=$1

by using var_dump($_GET); on the link localhost/user/1234/update I got

array (size=2)
'user' => string '1234' (length=4)
'action' => string 'update' (length=3)

while localhost/user/add

array (size=2)
'user' => string '' (length=4)
'action' => string 'update' (length=3)

which is my goal. I will just only do other stuffs under the hood with PHP.



Related Topics



Leave a reply



Submit