简体   繁体   English

如何使用SEO友好的apache URL更改php URLS?

[英]How to change php URLS with SEO-friendly apache URLS?

So I'm pulling events from the database such as /event-detail.php?id=1 , but I want it to look like /Event-Name/ instead. 所以我从数据库中提取事件,例如/event-detail.php?id=1 ,但我希望它看起来像/ Event-Name /。 How do I achieve this? 我该如何实现这一目标? I know a bit about mod Apache rewrite but not enough to know if it would work here. 我知道一些关于mod的重写但不足以知道它是否适用于此。 Or am I approaching this wrong? 或者我接近这个错误? Do I need to pull items with name instead? 我需要用名字来拉物品吗? /event-detail.php?name=Event Name ? /event-detail.php?name=Event Name I don't really want to do it that way even though it seems like it would be much easier. 我真的不想这样做,即使它看起来会更容易。

It would be very hard to make it work otherwise (I think). 如果不这样做会很困难(我想)。

Try this (write this to server conf or .htaccess): 试试这个(把它写到服务器conf或.htaccess):

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule ^(.*)$ event-detail.php?name=$1 [QSA,L] 
</IfModule>

That will rewrite urls and forward everything else than existing files and directories to event-detail.php script. 这将重写URL并将除现有文件和目录之外的所有内容转发到event-detail.php脚本。

Bit more info, if user call's /myShowEvent1 that will be rewritten as event-detail.php?name=myShowEvent1 or if user call's /event/25366273 that would be same as calling event-detail.php?name=event/25366273 (that is if they do not exists in the server). 更多信息,如果用户调用的/myShowEvent1将被重写为event-detail.php?name=myShowEvent1或者如果用户调用的/event/25366273与调用event-detail.php?name=event/25366273相同event-detail.php?name=event/25366273 (即如果它们不存在于服务器中)。

But remember all the links u have on the page must ba absolute not relative. 但请记住,您在页面上的所有链接必须绝对不是相对的。

Just try yourself and you will know what i am talking about. 试试自己,你会知道我在说什么。

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

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