简体   繁体   English

.htaccess mod使用php或其他自动重写?

[英].htaccess mod rewrite automatically using php or something?

I've got a number of users, each with the website page like http://www.mysite.com/folder/artist_id.php?id=33 我有很多用户,每个用户都有网站页面,例如http://www.mysite.com/folder/artist_id.php?id=33

The users need to be able to set their own easy URL such as http://www.mysite.com/userguy that would redirect to the above page. 用户需要能够设置自己的简单URL,例如http://www.mysite.com/userguy ,该URL会重定向到上面的页面。

I know how to write these out manually in .htaccess with RewriteRule but, for example, users have a whole control panel created with php and javascript and it's become necessary to allow them to choose the name themselves and have code automatically put in. But I don't have a clue if there's already an easy method of setting that up or adding to the .htaccess file via code or if I should just do some sort of file open, rewrite thing, or what's safe. 我知道如何使用RewriteRule在.htaccess中手动将其写出,但是例如,用户拥有一个使用php和javascript创建的整个控制面板,因此有必要让他们自己选择名称并自动输入代码。但是我如果已经有一种简便的方法可以通过代码设置或添加到.htaccess文件中,或者我只是应该进行某种类型的文件打开,重写操作或安全的操作,则不会有任何线索。

BTW, I'm not worried about issues such as them naming themselves the same as an existing folder or php file in my site. 顺便说一句,我不担心这样的问题,例如它们将自己命名为与我网站中的现有文件夹或php文件相同。 The users are limited and subject to approval so that shouldn't be a problem. 用户是有限的,并且需要批准,因此这不应该成为问题。

Or, redesign your database to handle natural keys instead of numeric ones. 或者,重新设计数据库以处理自然键,而不是数字键。
That'll save all your url de-mapping 33 = userguy etc, your urls will be more friendly, guessable and seo-friendly and browsing your database will be easier to boot. 这样可以节省所有的URL解映射33 = userguy等,您的URL将更加友好,可猜测和seo友好,并且浏览数据库将更易于启动。

username (PK)

You could set up one rewrite rule to send all requests like http://www.mysite.com/userguy to a single php script that then will do the "translation". 您可以设置一个重写规则,将所有请求(例如http://www.mysite.com/userguy)发送到单个php脚本,然后该脚本将执行“翻译”。 Doesn't necessarily have to be done via a rewrite rule, could also be an ErrorDocument handler . 不一定必须通过重写规则来完成,也可以是ErrorDocument处理程序 In this case the original request path can be found in $_SERVER['REQUEST_URI']. 在这种情况下,原始请求路径可以在$ _SERVER ['REQUEST_URI']中找到。

A proper solution that doesn't involve opening, modifying, and saving the .htaccess file using PHP would use a "router" of sorts. 一个不涉及使用PHP打开,修改和保存.htaccess文件的适当解决方案,将使用某种“路由器”。 Consider the following: 考虑以下:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

What this does is rewrite something like /userguy to /index.php?q=userguy, but only if /userguy isn't a directory or file that exists in the file system. 这样做是将/ userguy重写为/index.php?q=userguy,但前提是/ userguy不是文件系统中存在的目录或文件。 This happens transparently to the user. 这对用户是透明的。 index.php would then be responsible for do something like: 然后index.php将负责执行以下操作:

$path = $_GET['q'];
// Get router information using the $path variable.
$callback = get_callback_by_path($path);
// Assuming $callback is a function name. The function would print
// the page HTML or call into a theme layer or do whatever it needs to do.
call_user_func($callback);

The function get_callback_by_path would be custom logic that does whatever it needs to do to figure out what "userguy" needs to do. 函数get_callback_by_path将是自定义逻辑,它可以执行所需的操作来弄清楚“ userguy”需要做什么。 eg query a database mapping paths to function names and return the name of the function. 例如,查询数据库将路径映射到函数名称并返回函数名称。

There is of course more to it, but that's the idea. 当然还有更多,但这就是想法。

使用RewriteMap指令来调用脚本,以适当地转换URL。

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

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