简体   繁体   English

PHP MySQL-URL结构

[英]PHP MySQL - URL Structure

I have a semi-professional website about my travels where I present all the pictures for each country. 我有一个关于旅行的半专业网站,在那里我展示每个国家的所有图片。

For that I have a MySQL-Database containing the following attributes: - country - country-id - pictures (links) 为此,我有一个包含以下属性的MySQL数据库:-国家-国家/地区ID-图片(链接)

I am using a PHP-file which requests the pictures: "country.php" 我正在使用一个要求显示图片的PHP文件:“ country.php”

So I get the URL-Structure: www.url.com/country/spain/ or www.url.com/country/usa/. 因此,我得到了URL结构:www.url.com/country/spain/或www.url.com/country/usa/。

What I need is to change the URL Structure to www.url.com/spain but still maintain that "central" country.php-file. 我需要将URL结构更改为www.url.com/spain,但仍保留该“中央” country.php文件。

I know a method which only works, when I have a folder for each Country in my webspace and an index.php-file which simply loads the country.php-file (I did that on another website). 我知道只有在我的网站空间中有每个国家/地区的文件夹以及仅加载country.php-文件的index.php文件(我在另一个网站上做到了)的情况下,该方法才有效。 But in this case I would have way too many folders, which makes it all too complex since there are not only countries but also "special places and cities". 但是在这种情况下,我将拥有太多文件夹,这使它变得太复杂了,因为不仅有国家,而且还有“特殊的地方和城市”。

Is there another way to do that? 还有另一种方法吗?

EDIT: I got it with this code: 编辑:我得到了此代码:

RewriteEngine On

RewriteRule ^([a-zA-Z0-9-/]+)$ country.php?url=$1

But now the problem is, that whatever I type in, eg. 但是现在的问题是,无论我键入什么。 www.url.com/xyz, the server thinks that I want to open the country "xyz". www.url.com/xyz,服务器认为我要打开国家/地区“ xyz”。 What can I do, to only rewrite, when the entered country is in my database?. 当输入的国家/地区在我的数据库中时,该怎么办才能只重写?

I am sorry if I ask too complicated, but I just didn't know how explain my problem better. 很抱歉,如果我问得太复杂,但是我只是不知道如何更好地解释我的问题。

If you dont have any framework in use, which handles your routing, you could simply use the .htaccess file and set a rewrite rule. 如果没有使用任何用于处理路由的框架,则可以只使用.htaccess文件并设置重写规则。

Look here for example: how to remove folder name from url using htaccess 例如,在这里查看: 如何使用htaccess从网址中删除文件夹名称

$url ="******YOUR URL HERE***********";
$urlArr = parse_url($url);
$urlbase = $urlArr['host'] . @$urlArr['path']; 

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

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