简体   繁体   English

.htaccess用PHP重写URL

[英].htaccess url rewriting with php

I currently have an entire website running on PHP & GET variables. 我目前有一个整个网站都在运行PHP和GET变量。

My links look like this at present 我的链接目前看起来像这样

 http://www.example.co.uk/brochure.php?cat_path=24

And occasionally it has a second variable in the url... 偶尔在网址中还有第二个变量...

I actually would like my URLs to look like this: http://www.example.co.uk/Concrete_Fence_Posts 我实际上希望我的URL看起来像这样: http : //www.example.co.uk/Concrete_Fence_Posts

So my questions is simply, how do I go about rewriting urls without breaking my GET variables. 所以我的问题很简单,如何在不破坏GET变量的情况下重写网址。

Thanks. 谢谢。

Assuming your parameters are always named cat_path and product_id (if it exists) you can do something like this: 假设您的参数始终命名为cat_pathproduct_id (如果存在),则可以执行以下操作:

RewriteRule ^([^/]+)/([\d+])$         $1.php?cat_path=$2
RewriteRule ^([^/]+)/([\d+])/([\d+])$ $1.php?cat_path=$2&product_id=$3

Your URLs would then be in one of these formats: 您的网址将采用以下格式之一:

pagename/cat_path
pagename/cat_path/product_id

For example: 例如:

http://www.bentinckfencing.co.uk/brochure/24
http://www.bentinckfencing.co.uk/product/35/54

Edit: I see you want to use product names in the URL. 编辑:我看到您想在URL中使用产品名称。 In that case, your PHP scripts will need to be able to take a name as a parameter and look up the ID. 在这种情况下,您的PHP脚本将需要能够使用名称作为参数并查找ID。 You should continue to accept the ID directly so as to not break existing links. 您应继续直接接受该ID,以免破坏现有链接。 Then your rewrite rule would look like this: 然后,您的重写规则将如下所示:

RewriteRule ^([^.]+)$         brochure.php?name=$1

And http://www.bentinckfencing.co.uk/Concrete_Fence_Posts would rewrite to http://www.bentinckfencing.co.uk/brochure.php?name=Concrete_Fence_Posts . 并且http://www.bentinckfencing.co.uk/Concrete_Fence_Posts将重写为http://www.bentinckfencing.co.uk/brochure.php?name=Concrete_Fence_Posts

For better search search engine optimisation have URLs like stackoverflow - 为了更好地进行搜索引擎优化,请使用诸如stackoverflow之类的网址-

http://www.bentinckfencing.co.uk/brochure/24/concrete-fence-posts

There is something like search engines do not store whatever is after the ? 就像搜索引擎不会在?之后存储任何内容一样? in your URLs. 在您的网址中。 This ending concrete-fence-posts also helps users to identify the page in the browser address bar. 该结尾的concrete-fence-posts还帮助用户识别浏览器地址栏中的页面。 But, you cannot allow certain special characters if the ? 但是,如果?不允许使用某些特殊字符? is not used in the URL. URL中未使用。 Characters like / , % will break the URL and \\ , # , + will not appear. 诸如/%类的字符将破坏URL,并且\\#+将不会出现。

Updates 更新

Do you want to move completely from ID passing to name passing? 您想完全从ID传递转换为名称传递吗? I am not sure about how good that will be, but I have heard that a few well known sites have URLs like that. 我不确定这样做会有多好,但是我听说有些知名网站具有这样的URL。 But, you may have to do many changes depending on your project (how many modules you have). 但是,您可能必须根据项目(具有多少个模块)进行许多更改。 The stackoverflow style URL will, however, retain the ID based item fetching. 但是,stackoverflow样式的URL将保留基于ID的项目提取。

Further Updates 进一步更新
You can try this rewrite rule to have both ID and name in the URL (stackoverflow style):- 您可以尝试使用此重写规则在URL中使用ID和名称(stackoverflow样式):-

RewriteRule ^([a-z0-9\-\_]+)/([0-9]+)/([a-z0-9\-\_]+)/?$ $1.php?cat_path=$1&cat_name=$2

I have not tested it though. 我还没有测试过。

Another important thing, I see that your code enters brochure.php directly and there is no common file (common controller) which is executed in case of all the modules. 另一个重要的事情是,我看到您的代码直接输入了brochure.php ,没有所有模块都执行的通用文件(通用控制器)。 This approach can have many drawbacks, particularly if your's is a medium to large application. 这种方法可能有很多缺点,特别是如果您是中型到大型应用程序。 It is always better to execute a common index.php file and include the needed brochure.php file there so that you can perform all those common logics (needed in all your modules) in this index.php file. 最好执行一个公共index.php文件,并在其中包含所需的brochure.php文件,以便您可以在此index.php文件中执行所有那些通用逻辑(所有模块都需要)。 I have a common index.php file and the following rewrite rules do the same:- 我有一个公共的index.php文件,并且以下重写规则执行相同的操作:

############To remove display of "index.php" from URL
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php [L,QSA]
#################################################end of hiding index.php

RewriteRule ^([a-z0-9\-\_]+)/([0-9]+)/([a-zA-Z0-9\-\_]+)/?$ index.php?a=$1&b=$2&c=$3

Note:- The first block of rewrite rules hides the index.php from the URL and a visitor does not know that my application runs in PHP. 注意:-重写规则的第一块从URL中隐藏了index.php,访问者不知道我的应用程序是在PHP中运行的。

Finally I have URLs like this:- 最后,我有这样的URL:

http://www.bentinckfencing.co.uk/brochure/24/concrete-fence-posts

Hope this helps, 希望这可以帮助,
Sandeepan 桑迪潘

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

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