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!
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.
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.
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];
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With