简体   繁体   English

PHP基本网站页面链接

[英]Php basic website page link

I'm creating a simple php website with changeable themes. 我正在创建一个具有可变主题的简单php网站。 I have a directory called MyThemes with aa folder inside containing the following files header.php , sidebar.php , page.php , footer.php now the problem is that i want to display a page from the database using the page.php file of the selected theme, but the generated link will be something like 我有一个名为MyThemes的目录,里面有一个文件夹,其中包含以下文件header.phpsidebar.phppage.phpfooter.php现在的问题是我想使用的page.php文件显示数据库中的页面选定的主题,但生成的链接将类似于

website/MyThemes/ThemeName/page.php?id=somePageID

I want to change that if possible to something like 我想将其更改为类似

website/pages/somePageID

I have a little experience with PHP, but apparently not enough to do this. 我对PHP有一点经验,但显然不足以做到这一点。 So any help will be greatly appreciated. 因此,任何帮助将不胜感激。

This is typically done via URL rewriting by the webserver: the server makes sure /url/like/this is converted to (for instance) something.php?like=this . 通常,这是通过网络服务器通过URL重写完成的:服务器确保将/url/like/this转换为(例如) something.php?like=this After that there is no difference to the application. 之后,该应用程序没有任何区别。 Apache uses mod_rewrite to do this. Apache使用mod_rewrite来做到这一点。 If you were using Django, this would be configured in the urls.py files. 如果您使用的是Django,则将在urls.py文件中进行配置。

You could still simplify your URLs to website/pages.php?id=n by remembering the theme in a cookie or session variable though. 通过记住cookie或会话变量中的主题,您仍然可以简化URL到website/pages.php?id=n

Also, your current file paths suggest that you duplicated the pages for the different themes though: this is never a good idea. 另外,您当前的文件路径建议您尽管复制了不同主题的页面,但这绝不是一个好主意。 Don't repeat yourself! 不要重复自己!

You can use friendly urls and regular expressions: 您可以使用友好的网址和正则表达式:

See 看到

http://www.phpriot.com/articles/search-engine-urls http://www.phpriot.com/articles/search-engine-urls

In this article, you'll find instructions for using htaccess: 在本文中,您将找到有关使用htaccess的说明:

Example: 例:

<IfModule mod_rewrite.c>
RewriteEngine on
#rule not apply directories
RewriteCond %{REQUEST_FILENAME} !-d
#rule not apply files
RewriteCond %{REQUEST_FILENAME} !-f

#Rule to page 
RewriteRule ^page/$ page.php
RewriteRule ^page$ page.php
RewriteRule ^page/(.*)/(.*)?$ page.php?id=$1&des=$2
RewriteRule ^page/(.*)?$ page.php?id=$1
</IfModule>

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

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