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
MySQL Query to Get Column Names
MySQL VS MySQLi When Using PHP
Filter/Remove Rows Where Column Value Is Found More Than Once in a Multidimensional Array
Create Subdomains on the Fly With .Htaccess (PHP)
Creating One Array from Another Array in PHP
How to Convert an Image to Base64 Encoding
Htmlentities() Vs. Htmlspecialchars()
How to Convert Xml into Array in PHP
Download File to Server from Url
How to Post Json Data With PHP Curl
How to Run PHP from Windows Command Line in Wampserver
Creating Default Object from Empty Value in PHP
Return Single Column from a Multi-Dimensional Array
Curl Post Format For Curlopt_Postfields
Difference Between Require, Include, Require_Once and Include_Once
Convert a Series of Parent-Child Relationships into a Hierarchical Tree