Jump to content

.htaccess


jacko310592

Recommended Posts

hey guys,

im completely new to the .htaccess file, i need to find a way to rewrite URLs such as:

 

 

to:

 

can anyone suggest a way this can be done?

ive searched the net but what i find seems to be a bit confusing

 

 

thanks guys

Link to comment
Share on other sites

wow.  i had a total brain fart with that code.

 

try this:

 

RewriteEngine On
RewriteBase /

RewriteRule ^gallery/album/([a-zA-Z]+)/?$ gallery/album.php?page=$1 [NC,L]

 

EDIT: to explain the code a little bit:

 

the ([a-zA-Z]+) denotes a range where only characters a-z (as well as any uppercase A-Z) are allowed .. numeric values will return a 404.  the + denotes that anything is acceptable within that range.

 

the /? at the end means that the URL can be entered with or without a trailing forward slash.  if you kept the trailing slash and removed the ?, and then entered the same URL without a trailing slash, you would get a 404.  this way, it does not matter if there is a trailing slash or not.

 

and the $ after the /? means that we need an exact match, meaning that we must have a match in our URL must be exactly as follows:

 

'/gallery/album/anAlbumNameHere'

 

in the 2nd half of the rule, you will see a ?page=$1 .. the ?page works the exact same way as it would outside of .htaccess .. you call it like so: $_GET['page'] .. and you can attach more than just one, so:

 

?page=$1&id=$2&something=$3

 

will work granted you accommodate the $2 and $3 in the initial half of the rule .. you can go up to $9, and $0 returns the entire string.

 

the [NC,L] are conditions .. NC stands for NoCase, so the URL in questions becomes CaSE-iNSeNsiTiVE, and the L stands for Last, meaning that if this rule is met, the script stops and no more rules are checked.

 

hope you understand.

 

and usage would be using the $_GET superglobal to check for 'page' .. so:

 

create file in this structure: http://localhost/gallery/album.php

<?php
echo $_GET['page']; //will echo out whatever is in the URL after /album/
?>

 

for security reasons, you might want to create an array will is available to each page where you can check if that the page in question is ok, ie:

 

<?php
$arr = array ('alternative','edits','portraiture');

if (in_array ($_GET['page'], $arr))
{
     echo $_GET['page'];
}
else
{
     echo 'Incorrect page error goes here.';
}
?>

 

obviously a very simple example, but it's the jist.

 

and yes, that code goes into an .htaccess file, and can be placed in the root directory which makes it available to any directory within the root structure.

 

make sure when saving an .htaccess file that you select "All Files" or something similar and NOT ".txt Text Document", as the .htaccess file be appended with a .txt (.htaccess.txt) and it will not work.

 

any questions?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.