简体   繁体   English

htaccess mod_rewrite

[英]htaccess mod_rewrite

I'm trying to put something with this, whenever I go to a page like: 无论何时我去一个像这样的页面,我都想尝试一下这个:

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

I want the mod rewrite to change it to: 我希望mod重写将其更改为:

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

Which of course, the ID is the row of the character name... 当然,ID是字符名称的行...

For that kind of example... I've tried to work with it, but don't seem to get most of the production of htaccess because I haven't worked with .htaccess a lot really. 对于那种例子......我曾尝试使用它,但似乎没有得到htaccess的大部分生产,因为我没有真正使用过.htaccess

It's actually the other way around: 实际上反过来说:

  1. On your website, you write all your links the way you want them to look. 在您的网站上,您可以按照希望的方式编写所有链接。 For instance http://www.site.com/character/Jim_Carrey 例如http://www.site.com/character/Jim_Carrey

  2. In your database, you add a field, commonly called "slug" or "post_slug" which refers to the Jim_Carrey" part of the url. IT MUST BE A UNIQUE ELEMENT, much in the way of a primary key. So make sure you have a function that does take care of creating the slug based on a given string (the post title for example) and making sure there is no duplicate. 在你的数据库中,你添加了一个字段,通常称为“slug”或“post_slug”,它引用了url的Jim_Carrey“部分。它必须是一个独特的元素,很像主键。所以请确保你有一个函数,它负责根据给定的字符串(例如帖子标题)创建slug并确保没有重复。

  3. Then in your .htaccess (on the root folder) you do something like 然后在你的.htaccess(在根文件夹上)你做了类似的事情

     RewriteEngine On RewriteRule ^character/([a-z0-9_\\-]+)/$ character.php?slug=$1 [L,NC] 
  4. Finally, in your character.php script, you do a database query not against the ID, but against the post_slug field. 最后,在您的character.php脚本中,您执行的数据库查询不是针对ID,而是针对post_slug字段。

If you're only using the character's name, then something like the following would do 如果你只使用角色的名字,那么下面会有类似的东西

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

with a URL of eg http://www.example.com/character/Jim_Carrey . 使用例如http://www.example.com/character/Jim_Carrey的URL。 You'll then need to look up the character's name in the database using the slug passed in, as you won't have the ID to look it up with. 然后,您需要使用传入的slug在数据库中查找角色的名称,因为您将无法使用ID来查找。

Alternatively you could include the ID in the URL if you need it, vis: 或者,如果需要,您可以在URL中包含ID,vis:

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

This way you could have a URL like http://www.example.com/character/3/Jim_Carrey which would include the character name (for SEO reasons etc etc), but also the ID which you could then look up directly in your database. 通过这种方式,您可以使用http://www.example.com/character/3/Jim_Carrey这样的网址 ,其中包含字符名称(出于搜索引擎优化等原因等),还有您可以直接在您的网站中查找的ID数据库。

Edit a small PHP example for you re the first one: 为第一个编辑一个小的PHP示例:

<?php

  // ... database connection stuff here etc

  $slug = $_GET["slug"];

  // IMPORTANT: perform some data sanitization here
  // I'm just going to make sure it's 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 bound parameters where possible for your queries, and perform sanitization of your user data well. 与往常一样,尽可能使用绑定参数进行查询,并很好地执行用户数据的清理。 The PEAR MDB2 module has a nice page on how to do this here . PEAR MDB2模块有一个很好的页面,在这里有如何做到这一点

Edit 2 a quick and dirty setup :-) 编辑2快速而肮脏的设置:-)

.htaccess file as follows: .htaccess文件如下:

RewriteEngine On
RewriteRule ^character/(.*)$ /character.php?slug=$1
  • Your .htaccess file would ideally be in the root of your site. 您的.htaccess文件最好位于您网站的根目录中。 Eg /home/wayne/public_html/ or wherever your index file is served from 例如/ home / wayne / public_html /或者从哪里提供索引文件

  • A URL to match that would be http://www.example.com/character/Jim_Carrey - with the phrase "Jim_Carrey" appearing in your $_GET array as $_GET["slug"]. 匹配的URL将是http://www.example.com/character/Jim_Carrey - 在$ _GET数组中出现的短语“Jim_Carrey”为$ _GET [“slug”]。 NB apologies, wrote that PHP sleepy last night above so no wonder $_POST wouldn't work as its a GET request :-) I've updated it now! NB道歉,写道昨晚PHP困在上面,所以难怪$ _POST不会像GET请求一样工作:-)我现在已经更新了!

  • Finally you need to make sure that your host supports the use of .htaccess files. 最后,您需要确保主机支持使用.htaccess文件。 The setup of this is out of the scope of SO so any Apache configuration questions you'd be best asking over at https://serverfault.com/ 这个设置超出了SO的范围,因此任何Apache配置问题都是您最好在https://serverfault.com/上提出的问题。

I'm fairly certain you can't do this through htaccess. 我相当肯定你不能通过htaccess做到这一点。 You'll need to do it in PHP, querying the database using the information from the url (?id=3) and then calling the Header Function using what you've learned from the database. 您需要在PHP中执行此操作,使用url(?id = 3)中的信息查询数据库,然后使用从数据库中学到的内容调用Header Function

听起来你可以使用mod_rewrite rewritemap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM