如何制作干净的 URL [英] How to make Clean URLs

查看:18
本文介绍了如何制作干净的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,当我显示任何页面时,网址将更改为:https://www.asdgsdgsd.nl/index.php 或 about.php 或 contact.php等等...

I have a website, and when i display any page the url will change to: https://www.asdgsdgsd.nl/index.php or about.php or contact.php etc...

我已经看到您可以使用 .htaccess 创建干净的 url",但链接是:https://www.asdgsdgsd.nl/index.php?page=$1.

I have seen that you can create 'Clean urls' with .htaccess, but there the links were: https://www.asdgsdgsd.nl/index.php?page=$1.

我可以创建干净的网址吗?

is it possible for me to create clean urls?

我已经在我的根文件夹中的 .htaccess 文档中尝试过这个:

i have tried this in a .htaccess document on my root folder:

    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9]+)$ index.php

谁能帮帮我??你需要更多信息吗?只要问,我就会回答!!

Some one who can help me please?? you need some more information? just ask and i wil answer it!!

推荐答案

你有使用重写规则的想法,但你错过了最后一点.

You have the idea by using the rewrite rule, but you have missed out of the last bit.

RewriteRule ^([a-zA-Z0-9]+)$ index.php?id=$1

$1 将采用输入的第一个变量,因此您可以拥有 /index/example 并且单词 example 可以作为 _get['id']

The $1 will take the first varable entered so you can have /index/example and the word example can be pulled as a _get['id']

您可以通过这种方式创建多个:

You can create multipule in this way:

RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?id=$1&login=$2

等等.

更新:

如果您只是想让 URL 看起来更好,请点击此链接并阅读使用重写规则的所有不同方法.

If you just want to make the URL look nicer follow this link and read up on all the different ways of using the rewrite rule.

您可以更改http://www.pets.com/pet_care_info_07_07_2008.php

看起来更像这样:http://www.pets.com/pet-care/

如果这就是您要查找的内容,那么您要使用的规则是:

If thats what you are looking for then the rule you want to use is:

RewriteEngine On    # Turn on the rewriting engine
RewriteRule    ^pet-care/?$    pet_care_info_01_02_2008.php    [NC,L]    # Handle requests for "pet-care"

引自https://www. addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

RewriteRule"行是神奇发生的地方.该行可以分解为 5 个部分:

The "RewriteRule" line is where the magic happens. The line can be broken down into 5 parts:

RewriteRule - 告诉 Apache this like 指的是单个 RewriteRule.

RewriteRule - Tells Apache that this like refers to a single RewriteRule.

^/pet-care/?$ - 模式".服务器将检查对站点的每个请求的 URL,以查看此模式是否匹配.如果是,则 Apache 将交换请求的 URL 以用于后面的替换"部分.

^/pet-care/?$ - The "pattern". The server will check the URL of every request to the site to see if this pattern matches. If it does, then Apache will swap the URL of the request for the "substitution" section that follows.

pet_care_info_01_02_2003.php - 替代品".如果上述模式与请求匹配,Apache 将使用此 URL 而不是请求的 URL.

pet_care_info_01_02_2003.php - The "substitution". If the pattern above matches the request, Apache uses this URL instead of the requested URL.

[NC,L] - Flags",告诉 Apache 如何应用规则.在这种情况下,我们使用了两个标志.NC",告诉 Apache 这条规则不区分大小写,L"告诉 Apache 如果使用了这条规则,不要再处理任何规则.

[NC,L] - "Flags", that tell Apache how to apply the rule. In this case, we're using two flags. "NC", tells Apache that this rule should be case-insensitive, and "L" tells Apache not to process any more rules if this one is used.

# 处理宠物护理"请求 - 解释规则作用的评论(可选但推荐)

# Handle requests for "pet-care" - Comment explaining what the rule does (optional but recommended)

这篇关于如何制作干净的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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