Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将PHP文件用于多个URL_Php_Html_Wordpress - Fatal编程技术网

如何将PHP文件用于多个URL

如何将PHP文件用于多个URL,php,html,wordpress,Php,Html,Wordpress,我希望有一个PHP文件,负责子目录中的多个URL 例如,我的网站是。我想要一个php文件,比如说,名为playlist.php,它可以处理用户何时进入或是否进入等。我希望能够将数字(上面的示例URL中的101、142)作为变量(playlist ID)使用,以便显示正确的播放列表 我知道我可以在我的playlist子目录中创建一个index.php,然后使用GET变量,这样就可以得到ID,但是这看起来很草率,我想知道如何用另一种方法来做 我的网站是建立在WordPress上的,但我不认为这会有任

我希望有一个PHP文件,负责子目录中的多个URL

例如,我的网站是。我想要一个php文件,比如说,名为
playlist.php
,它可以处理用户何时进入或是否进入等。我希望能够将数字(上面的示例URL中的101、142)作为变量(playlist ID)使用,以便显示正确的播放列表

我知道我可以在我的playlist子目录中创建一个
index.php
,然后使用
GET
变量,这样就可以得到ID,但是这看起来很草率,我想知道如何用另一种方法来做


我的网站是建立在WordPress上的,但我不认为这会有任何不同。

好吧,单靠PHP是无法做到这一点的

  • 如果使用Apache,则可以使用.htaccess
  • 如果使用IIS,则可以使用URL重写
这些模块背后的基本思想是从一个URL映射到另一个URL。例如:您可能希望从中映射

http://www.startingtofeelit.com/playlist/142 =>
http://www.startingtofeelit.com/playlist.php?id=142
您可以用正则表达式表示URL映射。例如,in.htaccess(Apache)。你可以这样写

RewriteRule    ^playlist/([0-9]+)/?$    playlist.php?id=$1
注意,您的网站目录中需要有.htaccess文件。由于您使用的是Wordpress,您存在的可能性很高。您只需将该行代码附加到existed.htaccess

下面是对正则表达式的解释:

^playlist/      # any URL start with playlist/
([0+9]+)        # following by number, and store it as $1
/?$             # end with or without /
映射到

playlist.php?id=$1  # where $1 is taken from the matched number from our pattern.

好吧,单靠PHP是无法做到这一点的

  • 如果使用Apache,则可以使用.htaccess
  • 如果使用IIS,则可以使用URL重写
这些模块背后的基本思想是从一个URL映射到另一个URL。例如:您可能希望从中映射

http://www.startingtofeelit.com/playlist/142 =>
http://www.startingtofeelit.com/playlist.php?id=142
您可以用正则表达式表示URL映射。例如,in.htaccess(Apache)。你可以这样写

RewriteRule    ^playlist/([0-9]+)/?$    playlist.php?id=$1
注意,您的网站目录中需要有.htaccess文件。由于您使用的是Wordpress,您存在的可能性很高。您只需将该行代码附加到existed.htaccess

下面是对正则表达式的解释:

^playlist/      # any URL start with playlist/
([0+9]+)        # following by number, and store it as $1
/?$             # end with or without /
映射到

playlist.php?id=$1  # where $1 is taken from the matched number from our pattern.

这通常是以类似于您已经尝试过的方式处理的。但是,通常使用重写脚本,以便应用程序接受干净的URL,例如:

…并为您的应用程序重新编写:

例如,如果您使用的是Apache web服务器,并且安装并启用了mod_rewrite模块,那么您可以在.htaccess文件中使用以下代码段,并使用GET参数,正如您已经知道的那样。其他流行的web服务器都有独特的URL重写模块,可以让您执行相同的操作

<IfModule mod_rewrite.c>
RewriteEngine On
# Rewrite this:
# http://www.example.com/somepage/1
# ...into this:
# http://www.example.com/somepage?id=1
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

重新启动发动机
#重写以下内容:
# http://www.example.com/somepage/1
#……在这方面:
# http://www.example.com/somepage?id=1
重写基/
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php?/$1[L]

这通常与您已经尝试过的方法类似。但是,通常使用重写脚本,以便应用程序接受干净的URL,例如:

…并为您的应用程序重新编写:

例如,如果您使用的是Apache web服务器,并且安装并启用了mod_rewrite模块,那么您可以在.htaccess文件中使用以下代码段,并使用GET参数,正如您已经知道的那样。其他流行的web服务器都有独特的URL重写模块,可以让您执行相同的操作

<IfModule mod_rewrite.c>
RewriteEngine On
# Rewrite this:
# http://www.example.com/somepage/1
# ...into this:
# http://www.example.com/somepage?id=1
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

重新启动发动机
#重写以下内容:
# http://www.example.com/somepage/1
#……在这方面:
# http://www.example.com/somepage?id=1
重写基/
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php?/$1[L]

因此,您已经用普通php编写了playlist.php,工作正常,现在需要将其包含在wordpress网站下。对吗?我甚至还没有编写playlist.php。首先,我想知道如何告诉我的服务器,像www.startingtofeelit.com/playlist/NUMBER这样的任何东西都应该调用playlist.php脚本并向其提供号码。我可以自己处理playlist.php的其余编码,所以您已经用普通php编写了playlist.php,工作正常,现在您需要将其包含在wordpress网站下。对吗?我甚至还没有编写playlist.php。首先,我想知道如何告诉我的服务器,像www.startingtofeelit.com/playlist/NUMBER这样的任何东西都应该调用playlist.php脚本并向其提供号码。我可以自己处理playlist.php的其余编码,WordPress也通过.htaccess和mod_rewrite发挥了自己的魔力。看,很好,我让它工作了。我只需要确保我在WordPress设置的
重写规则的其余部分之前就完成了。尤达曼!WordPress还通过.htaccess和mod_重写实现了自己的魔力。看,很好,我让它工作了。我只需要确保我在WordPress设置的
重写规则的其余部分之前就完成了。尤达曼!