删除扩展名为.php的网址 [英] Removing .php extension in URLs

查看:222
本文介绍了删除扩展名为.php的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我previously改变了我的的.htaccess 文件,这样我的子目录(13 /),可以是目录我通过增加用我的主要领域

 的RewriteCond%{HTTP_HOST} ^(WWW)?mydomain.com $
重写规则^(/)?$ 13 /的index.php [L]
 

现在我想删除的.php 的扩展和延伸,比如的index.php?事件ID = 3 指数/某事

编辑#1 [其中,的东西 ID名称存储数据库]

我应该怎么办呢? previously,我在一些PHP的框架做了工作,但他们已经有一个专门的网​​址文件。所以,我从来没有想过太多了。但是这一次,我没有使用任何框架,所以我需要修改的.htaccess

我看了看四周,但未能找到任何可靠的博客/来源。

编辑#2

我的子目录是的public_html / 13 /

例如, www.mydomain.com 重定向到 www.mydomain.com/13 / 现在,我要访问 www.mydomain.com/13/events.php?eventid=1 www.mydomain.com/events/44

解决方案

 重写规则^([A-ZA-Z0-9 \  - ] +)/([A-ZA-Z0 -9 \  - ?] +)/([0-9] +)/ $ $ 1.PHP $ 2 = $ 3
 

以上code会重定向 http://www.mydomain.com/index /事件ID / 3 / http://www.mydomain.com/index的.php?事件ID = 3

htaccess是非常灵活的,一旦你理解了语法产生的上述变化是pretty的方便。您将要读上常规的前pressions就可以使用,并且选项重写模块所提供的无数。我的大多数重定向的样子,虽然上面的,真正的简单。

编辑:您要查找的内容的一个例子是字preSS,它使用蛞蝓的基础上的东西比一个ID号更多的语义识别的网页。你必须给url烯code从数据库中的项目的名称,要么将其存储在数据库中作为团状,或发现使得它在检索页面的内容的数据库查询usuable的一些其他的方式。那么你的htaccess会是这个样子:

 重写规则^([A-ZA-Z0-9 \  - ] +)/([A-ZA-Z0-9 \  - ] +)/ $ $ 1.PHP?塞= $ 2
 

在你的PHP,你将需要创建蛞蝓并将其存储在数据库中,以便它们可以被用来作为查询的ID。

EDIT2:试着改变你的整个htaccess文件,只是这样的:

 < IfModule mod_rewrite.c>
RewriteEngine叙述上

的RewriteCond%{HTTP_HOST} ^ WWW \ .yourdomain \ .COM $ [NC]
重写规则^(。*)$ http://yourdomain.com/$1 [R = 301,L]

重写规则^([A-ZA-Z0-9 \  - ] +)/([A-ZA-Z0-9 \  - ] +)?/([0-9] +)/ $ $ 1.PHP $ 2 = $ 3

< / IfModule>
 

我已经测试了它和它的作品。别的东西在你的htaccess是搞乱起来。

I previously changed my .htaccess file so that my 'subdirectory' (13/) can be the 'directory' I use for my main domain by adding :

RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteRule ^(/)?$ 13/index.php [L]

Now I want to remove the .php extensions and extensions like index.php?eventid=3 to index/something

EDIT#1[where something is the name of that id stored in the database]

How should I do that? Previously, I did work in some PHP-frameworks but they already have a dedicated url file. So I never thought much about it. But this time, I'm not using any framework, so I need to modify .htaccess.

I looked around, but was unable to find any reliable blog/sources.

EDIT #2

My sub-directory is public_html/13/.

ie, www.mydomain.com redirects to www.mydomain.com/13/ Now I want to access www.mydomain.com/13/events.php?eventid=1 as www.mydomain.com/events/44

解决方案

RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)/?$ $1.php?$2=$3

The above code would redirect http://www.mydomain.com/index/eventid/3/ to http://www.mydomain.com/index.php?eventid=3

htaccess is very flexible, and creating variations of the above is pretty easy once you understand the syntax. You will want to read up on the regular expressions you can use, and the myriad of options the Rewrite module has to offer. Most of my redirects look like the above though, real simple.

EDIT: An example of what you're looking for is Wordpress, which uses slugs to identify pages based on something more semantic than an ID number. You would have to url encode the name of the item from the database and either store it in the database as the slug, or find some other way of making it usuable in the database query that retrieves the content of the page. Then your htaccess would look something like this:

RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/?$ $1.php?slug=$2

In your PHP you will need to create slugs and store them in the db so they can be used as IDs in your queries.

EDIT2: Try changing your entire htaccess file to just this:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]

RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)/?$ $1.php?$2=$3

</IfModule>

I have tested it and it works. Something else in your htaccess is messing it up.

这篇关于删除扩展名为.php的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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