How to Create Clean Url Using .Htaccess

How to create clean url using .htaccess

Replace your code with this:

ErrorDocument 404 /404.php
AddDefaultCharset UTF-8
Header unset ETag
FileETag None

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+movie\.php\?name=([^\s&]+) [NC]
RewriteRule ^ movie/%1? [R=301,L]

RewriteRule ^movie/([^/]+)/?$ movie.php?name=$1 [L,QSA]

How to create Clean URL for every page using .Htaccess?

You mentioned WordPress, which has something like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

What this does is redirect any request that doesn't match a real file or directory to the index.php script. That script will then decide what page to display. It will do so by looking into $_SERVER['REQUEST_URI'] which holds a path like register/part1.

It would be easier for you to figure out what page to show using this method, because in PHP there are many ways to parse that path string, then map it to a function

How to create clean url using .htaccess for multiple parameters

You just need to add a parameter in your rule

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+download\.php\?id=([0-9]+)&name=([^\s&]+)\s [NC]
RewriteRule ^ download/%1/%2? [R=301,L]
RewriteRule ^download/([0-9]+)/([^/]+)/?$ download.php?id=$1&name=$2 [L]

clean URL using .htaccess

Just append this rule at the end of your existing .htaccess file:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?dir=([^\s]+) [NC]
RewriteRule ^ %1? [R=301,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