Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create friendly URLs with .htaccess?

I'm having a hard time with .htaccess. I want to create friendly URLs for a site I'm working on...

Basically I want to convert this:

http://website.com/index.php?ctrl=pelicula&id=0221889
http://website.com/index.php?ctrl=pelicula&id=0160399&tab=posters

Into this:

http://website.com/pelicula/0221889/
http://website.com/pelicula/0221889/posters/

In case I need it later I would also want to know how to add the article title to the end of the URL like this (I'm using PHP):

http://website.com/pelicula/0221889/the-article-name/
http://website.com/pelicula/0221889/the-article-name/posters/

Note: Stackoverflow method is also good for me, for example the url of this question is:

http://stackoverflow.com/questions/3033407/htacces-to-create-friendly-urls-help-needed

But you can put anything after the id and it will also work. like this:

http://stackoverflow.com/questions/3033407/just-anything-i-want

I have used some automatic web tools for creating the .htacces file, but its not working correctly. So I ask for your help.

I will also be glad if you can recommend .htacces best practices and recommendations..

EDIT: based on some answers I get here I put this:

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

But I get the default host 'page not found' error.

I also tried:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/(\d+)/([^/]+)/?$ index.php?ctrl=$1&id=$2&tab=$3 [QSA,L]
    RewriteRule ^([^/]+)/(\d+)/?$         index.php?ctrl=$1&id=$2 [QSA,L]
    RewriteRule ^([^/]+)/?$               index.php?ctrl=$1 [QSA,L]
</IfModule>

This also does not work. It takes me to my default 404.php page.

mod_rewrite is enabled and working.

Help!

like image 363
Jonathan Avatar asked Jun 13 '10 18:06

Jonathan


People also ask

How to make clean URLs with PHP and. htaccess?

Steps to Use Mod_Rewrite:Create a file and save it as a. htaccess, then switch on the rewrite engine in the. htaccess file by adding RewriteEngine on Then after that Create Rules to match URL requests for clean URL's.

What is a friendly URL example?

Friendly URLs are called Aliases in Sitecore. The benefit of creating a friendly URL is that they are easier to remember and contain key words about the web page. Example: Original URL: https://portal.ct.gov/Services/Education/Higher-Education/Higher-Education-Information-and-Resources.


2 Answers

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];
like image 153
Richard JP Le Guen Avatar answered Oct 07 '22 00:10

Richard JP Le Guen


Just tested this locally and it seems to do what you need:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/.]+)(?:/)?$ /index.php?ctrl=$1 [L]
    RewriteRule ^([^/.]+)/([^/.]+)(?:/)?$ /index.php?ctrl=$1&id=$2 [L]
    RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)(?:/.*)?$ /index.php?ctrl=$1&id=$2&tab=$3 [L]
</IfModule>

This is what it does:

  • For a path like http://website.com/pelicula/, redirect to /index.php?ctrl=pelicula
  • For a path like http://website.com/pelicula/0221889, redirect to /index.php?ctrl=pelicula&id=0221889
  • For a path like http://website.com/pelicula/0221889/posters/, redirect to /index.php?ctrl=pelicula&tab=posters
  • For a path like http://website.com/pelicula/0221889/posters/anything-can-go-here, redirect to /index.php?ctrl=pelicula&tab=posters and ignore the rest of the path.

These rewrites will work with or without a trailing slash - if you want them to work only without the slash, remove the (?:/)? from the end of the first two rewrites. You can also use mod_rewrite to add or remove a trailing slash as required.

like image 29
cam8001 Avatar answered Oct 07 '22 01:10

cam8001