简体   繁体   English

htaccess用于动态网页

[英]htaccess for dynamic web pages

I have a website and i am using MySQL to store and fetch data from, there is a bunch of data of different destinations (Yes this is a travel agent website) i am wondering how can i setup .htaccess file to display SEO friendly URL 我有一个网站,我使用MySQL来存储和获取数据,有一堆不同目的地的数据(是的,这是一个旅行社网站),我想知道如何设置.htaccess文件以显示SEO友好URL

For example: http://www.mywebsite.com/flights-result.php?id=10 this URL is a details page for a flight to Entebbe in Africa, i would like to have the URL for this like http://www.mywebsite.com/Africa/Entebbe.htm 例如: http : //www.mywebsite.com/flights-result.php?id=10此URL是飞往非洲恩德培的航班的详细信息页面,我希望拥有类似http://的URL www.mywebsite.com/Africa/Entebbe.htm

And so on for them, one more thing do i need to add this for every page? 等等,对于他们来说,我还需要为每一页添加一件事吗? the data is being update on daily basis so is there any easy way to write URL automatically? 数据每天都在更新,是否有任何简单的方法可以自动编写URL?

Any help highly appreciated. 任何帮助,高度赞赏。

I don't really think what you are trying to accomplish has anything to do with mysql. 我真的不认为您要完成的工作与mysql有关。 What you are looking for is called URL rewriting. 您正在寻找的被称为URL重写。 There are countless number of articles out there that could show you the direction to follow. 那里有不计其数的文章可以告诉您要遵循的方向。 I am not very sure which web server you are using right not. 我不太确定您使用的是哪个Web服务器。 I presume it is Apache. 我认为它是Apache。 Here is Apache module_rewrite guide. 是Apache module_rewrite指南。

Given the original URL, there isn't all the information in there to use mod_rewrite to do this completely. 在给出原始URL的情况下,并没有那里的所有信息都可以使用mod_rewrite来完全完成此操作。

So what you could do it send all web requests to a controller file, and from there parse the request uri and load the correct page. 因此,您可以做什么将所有Web请求发送到控制器文件,然后从那里解析请求uri并加载正确的页面。

So in htaccess, something like... 所以在htaccess中,类似...

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ controller.php [L]

Then in controller.php you parse the url and load the correct page. 然后,在controller.php中解析URL并加载正确的页面。


A different option you may prefer, (if you're flexible on the specific final URL) is to have URLs ending up looking like this: 您可能会喜欢的另一种选择是(如果您对特定的最终URL灵活的话)是使URL最终看起来像这样:

http://www.mywebsite.com/flights/10/Africa/Entebbe.htm

This would likely be simpler to do instead of implementing a controller (although I prefer the controller for routing requests). 这可能比实现控制器更简单(尽管我更喜欢使用控制器来路由请求)。

So in htaccess... 所以在htaccess中...

RewriteRule
    ^/flights/([0-9]{1,10})/([a-zA-Z]+)/([a-zA-Z]+)\.htm$
    flights-result.php?id=$1&country=$2&place=$3 [L]

Then near the start of the flights-results.php file you should load the data for the id, then check that the provided "country" and "place" are correct (to stop people just entering anything here), and return a 4040 if it's not. 然后,在flights-results.php文件的开头附近,您应该加载ID数据,然后检查提供的“国家”和“地点”是否正确(以阻止人们在此处输入任何内容),如果返回4040,不是。

Remember to change all the links your app outputs to the new style as well. 切记也将您的应用输出的所有链接也更改为新样式。


You could also, as you mentioned, hard code all these URLs into a htaccess, but that's not ideal :) 正如您提到的,您也可以将所有这些URL硬编码为htaccess,但这并不理想:)

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

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