简体   繁体   English

需要帮助复制 .htaccess mod_rewrite 重定向 function 在 php 像 ZDCEC9B13C12DE51501ZA3BC0

[英]Need help to replicate .htaccess mod_rewrite redirection function in php like Wordpress

We all know that wordpress have simple .htaccess code like below RewriteEngine on RewriteBase /我们都知道 wordpress 有简单的 .htaccess 代码如下 RewriteEngine on RewriteBase /

# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s 

# pass the rest of the request into index.php to handle     
RewriteRule ^(.*)$ /index.php/$1 [L]

But if I redirect all requests to index.php, I think it becomes pretty cumbersome to handle every rewrite in php.但是,如果我将所有请求重定向到 index.php,我认为处理 php 中的每次重写都会变得非常麻烦。 Currently I have a logic in mind, like maintain a db table with all valid redirections.目前我有一个逻辑,比如维护一个包含所有有效重定向的数据库表。 But still I don't know how to handle rules likes ([0-9]+).但我仍然不知道如何处理规则喜欢 ([0-9]+)。

If anyone has implemented something like this before or has a logic in mind, can you please guide me in this matter如果有人以前实施过这样的事情或有逻辑,请你指导我这件事

The sole purpose am doing this, is because I want flexibility in adding/deleting categories in the menu of my site.这样做的唯一目的是因为我希望灵活地在我的网站菜单中添加/删除类别。 I don't want to go to .htaccess every time and edit it at all places.我不想每次都从 go 到 .htaccess 并在所有地方进行编辑。 I want to create more like a CMS where user can add delete categories我想创建更像一个 CMS,用户可以在其中添加删除类别

I don't understand the question, WordPress already handles all this for you.我不明白这个问题,WordPress 已经为你处理了这一切。 Unless you mean you aren't using WordPress?除非你的意思是你没有使用 WordPress? In which case yes, you can do it either way.在这种情况下是的,你可以这样做。 What kind of URL structure do you want?你想要什么样的URL结构? You could write a rule like so:你可以像这样写一个规则:

RewriteRule ^category/(.*)$  categories.php?cat=$1 [L]

To make a URL like domain.com/category/dogs rewrite to domain.com/categories.php?cat=dogs.要使 URL 像 domain.com/category/dogs 重写到 domain.com/categories.php?cat=dogs。 Obviously you can adjust this to your liking, and write a few more similar rules for tags, posts etc.显然你可以根据自己的喜好调整它,并为标签、帖子等编写更多类似的规则。

Handling routing in php would be a more dynamic and 'elegant' solution.在 php 中处理路由将是一个更加动态和“优雅”的解决方案。 You could try using a framework like CodeIgniter, this will manage routes for you automatically and make it easy to define custom routes.您可以尝试使用 CodeIgniter 之类的框架,这将自动为您管理路由并轻松定义自定义路由。 Probably better than writing a bunch of .htaccess rules.可能比写一堆 .htaccess 规则要好。

My suggestion would be to set up php based routing the basic idea is that you do something like this:我的建议是设置基于 php 的路由,基本思想是你做这样的事情:

RewriteEngine On

# skip all files with extensions other than .html
# this way if you want you can append the .html to dynamic pages
# which wont really exist as a file
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]

// redirect everything to your front controller
RewriteRule ^(.*)$ index.php [QSA,L]

Notice how you just make everythign go to index.php but you dont rewrite any of the variables or anything.请注意,您如何将所有 go 设置为index.php ,但您不会重写任何变量或任何东西。 This is because you will work out what to based on the pattern of the URL with php.这是因为您将根据 URL 和 php 的模式计算出要做什么。 In order to do this youll need to implement a router.为此,您需要实现一个路由器。 Basically a router takes a request and matches it against a pattern and then determines any parameters based on the pattern.基本上,路由器接受请求并将其与模式匹配,然后根据模式确定任何参数。

There are existing libraries out there to do this for you.现有的库可以为您执行此操作。 For example Zend_Controller_Router .例如Zend_Controller_Router

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

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