English 中文(简体)
使用 PHP 清除的 URL [重复]
原标题:Clean URLs with PHP [duplicate]

因此,我正试图在PHP中建立一个干净的url系统,以改变以下的URLS: http://example.com/index.php?projects=05,使: http://example.com/projects/05

到目前为止,我已经知道如何使用 parse_url 来绘制看上去像 http://example.com/index.php/ projects/05 的urls,但我还不知道如何从 URL 中删除 index.php。是否有办法使用.htaccess从 URL 字符串中删除 index.php

我知道这是一个简单的问题 但经过大量勾引,我找不到解决办法

最佳回答

您需要使用 mod_ rewrite 在 Apache 中这样做。 您需要将所有的 URL 重新定位到索引. php, 然后, 也许使用 press_ url, 找出如何处理它们 。

例如:

# Turn on the rewrite engine
RewriteEngine On

# Only redirect if the request is not for index.php
RewriteCond %{REQUEST_URI} !^/index.php

# and the request is not for an actual file
RewriteCond %{REQUEST_FILENAME} !-f

# or an actual folder
RewriteCond %{REQUEST_FILENAME} !-d

# finally, rewrite (not redirect) to index.php
RewriteRule .* index.php [L]
问题回答

我正在使用以下的.htaccess 文件来删除 url 的索引.php 部分 。

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /

# Protect hidden files from being viewed
<Files .*>
    Order Deny,Allow
    Deny From All
</Files>

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !favicon.ico$

RewriteRule .* index.php/$0 [PT]

否则,我可以推荐Kohana框架作为参考(它们也有一个相当不错的url 采集器和控制系统)

将实际文件/文件夹与 URL 脱钩的概念称为 < em> routing 。 许多 PHP 框架包括这种功能, 大多使用 < a href=" https:// https:// httpd.apache.org/docs/ current/restrite/ "rel=" "nofollow" >mod_rewrite 。 在 < a href=" http://josephscott.org/archives/2008/11/php-url-routing-pur/" rel="nofolpol" > PHP URL Routing 上有一个漂亮的博客文章, 执行简单的独立路由器类 。

它创造出像这样的绘图:

mysite.com/projects/show/1 --> Projects::show(1)

因此,所请求的 URL 产生类的 < code>show () 函数 < code> projects 被调用, 参数为 < code> 1 。

您可以使用它来建立您 PHP 代码中漂亮 URL 的灵活映射 。

像这样的东西 在你的.httaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

(确保启用重写模块)





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签