Htaccess mod_rewrite

I try to do something about it whenever I go to a page, for example:

http://www.example.com/character.php?id=3 

I want mod rewrite to change it to:

 http://www.example.com/character/Jim_Carrey 

Which, of course, the identifier is a character name string ...

For such an example ... I tried to work with it, but did not seem to get much of the production of htaccess, because I did not work very well with .htaccess .

+2
source share
4 answers

Actually the opposite:

  • On your site, you write all your links the way you want them to look. For example http://www.site.com/character/Jim_Carrey

  • In your database, you add a field, usually called the "slug" or "post_slug", which refers to the Jim_Carrey "part of the URL. IT MUST BE A UNIQUE ITEM, pretty much the primary key. So make sure you have a function that really takes care of creating bullets based on the given string (message header for example) and make sure there is no duplicate.

  • Then in your .htaccess (in the root folder) you do something like

     RewriteEngine On RewriteRule ^character/([a-z0-9_\-]+)/$ character.php?slug=$1 [L,NC] 
  • Finally, in your character.php script file, you are executing a database query, not against the ID, but against post_slug.

+5
source

If you use only the symbol name, then something like the following:

RewriteRule ^ character /(.*)$/ character.php? Slug = $ 1

with a url like http://www.example.com/character/Jim_Carrey . Then you will need to find the name of the symbol in the database using the missing pass, since you will not have an identifier to look at it.

Alternatively, you can specify the identifier in the URL, if you need it, in relation to:

RewriteRule ^ character / ([0-9] +) /.*$/ character.php? Id = $ 1

So you can have a url like http://www.example.com/character/3/Jim_Carrey , which will include the name of the character (for SEO reasons, etc.), but also the identifier that you could find directly in their database.

Change a small PHP example for you:

 <?php // ... database connection stuff here etc $slug = $_GET["slug"]; // IMPORTANT: perform some data sanitization here // I'm just going to make sure it only letters, numbers and // hyphens/underscores as an example. $slug = preg_replace("/[^a-zA-Z0-9\-_]+/", "", $slug); // Now look up in your database // Ideally you'd have a slug column to compare this with, that you can fill // when your record is created/updated // You'd also be best off using bound parameters here, rather than directly // adding the data into the query string. I personally use the MDB2 PEAR // module but feel free to use whatever you normally use. $qry = "SELECT * FROM characters WHERE slug='" . $slug . "'"; $result = mysql_query($qry, $db); // do something based on this result, fail if none found // or show details if OK etc // ... ?> 

Hope this helps! As always, use related parameters where possible for your needs, and do a good job of sanitizing your user data. The PEAR MDB2 module has a nice page on how to do this here .

Change 2 quickly and dirty :-)

.htaccess:

 RewriteEngine On RewriteRule ^character/(.*)$ /character.php?slug=$1 
  • Your .htaccess file will ideally be at the root of your site. For example / home / wayne / public_html / or wherever your index file is filed from

  • The URL that will match http://www.example.com/character/Jim_Carrey - with the phrase Jim_Carrey appearing in your $ _GET array as $ _GET ["slug"]. NB apologize, wrote that PHP is sleepy last night above, so it is not surprising that $ _POST will not work as its GET request :-) I updated it now!

  • Finally, you need to make sure your host supports the use of .htaccess files. Setting this up is not part of the SO scope, so any Apache configuration questions that you are best asked are https://serverfault.com/

+3
source

I am sure that you cannot do this through htaccess. You will need to do this in PHP by querying the database using the information from url (? Id = 3), and then call the Header Function using what you learned from the database.

0
source

It looks like you could use mod_rewrite rewritemap .

0
source

All Articles