我怎样才能创建的.htaccess友好的URL? [英] How can I create friendly URLs with .htaccess?

本文介绍了我怎样才能创建的.htaccess友好的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个困难时期的.htaccess。我想创建一个网站我工作友好的URL ......

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/

在情况下,我需要它后,我也想知道如何将文章标题添加到像这样的URL的末尾(我使用PHP):

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/

注:#1的方法也是为我好,比如这个问题的网址是:

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

但你可以在ID后面放任何东西,它也将正常工作。像这样的:

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

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

我已经使用了一些自动化的网络工具创建.htacces文件,但它不能正常工作。所以,我找你帮忙。

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

我也很高兴,如果你能推荐.htacces最佳做法和建议。

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

编辑:根据一些答案我来到这里,我把这样的:

<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.

我也试过:

<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>

这也不起作用。它带我到我的默认404.php页。

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

mod_rewrite的启用和工作。

mod_rewrite is enabled and working.

帮助!

推荐答案

在文档根目录 http://website.com/ 我把一个的htaccess 文件是这样的:

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>

然后在你的PHP脚本,你可以操纵 $ _ GET ['URL'] 变量,请您:

$path_components = explode('/', $_GET['url']);
$ctrl=$path_components[0];
$id=$path_components[1];
$tab=$path_components[2];

这篇关于我怎样才能创建的.htaccess友好的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆