问题描述
我所要求的只是将任何给定 URL 的子目录重写为 PHP URL 参数,以获得更美观、用户友好的 URL.
What I'm asking for is simply rewriting any URL-given sub-directiories to a PHP URL parameter for nicer looking, user friendly URL.
我知道使用 Apache 重写引擎和相关参数可以做到这一点.我的问题是我不想重写,即
I know that this is possible using the Apache Rewrite Engine and associated parameters.The problem on my end is that I do not want to rewrite i.e
mydomain.com/parameter ----------->
mydomain.com/index.php?id=parameter
但是重写
mydomain.com/subdirectory/parameter ----------->
mydomain.com/subdirectory/index.php?id=parameter
在我的根文件夹中,我有一个 .htaccess 文件,用于 404 错误等,将不存在的页面重定向到主页面.在我完成的测试期间,我停用了这些,认为它们可以覆盖其他 .htaccess 文件.我试过了...
In my root folder I have an .htaccess file for 404 errors and such, redirecting non-existant pages to the main one. During the tests I've done I have deactivated these thinking that they could overwrite the other .htaccess file.I tried...
RewriteEngine On
RewriteRule ^directory/([^/\.]+)/?$ index.php?id=$1 [L]
...以及其他类似的片段.
...and other snippets similar to that one.
包含此代码的 .htaccess 文件位于根目录/目录文件夹中.我想要实现的是将 root/directory/string 重定向到 root/directory/index.php?id=string.
The .htaccess file containing this code was placed in the root/directory folder.What I wanted to achieve with that was to redirect root/directory/string to root/directory/index.php?id=string.
由于我对 .htaccess 和重写的知识显然有限,我需要回答两个问题.
Since my knowledge of .htaccess and rewriting is obviously limited, I need answers to two questions.
根文件夹中的 404 重写器是否会覆盖子目录中的任何其他重写器?
Will a 404 rewriter in the root folder overwrite any other rewriter in a subdirectory?
我如何实现我所追求的目标?(很像 url-shorteners 的工作原理 - bit.ly/shortened)来自子目录?
How do I achieve what I'm after? (much like how url-shorteners work - bit.ly/shortened) from a subdirectory?
推荐答案
此规则应该适用于您的 DOCUMENT_ROOT/.htaccess
文件:
This rule should work for you in your DOCUMENT_ROOT/.htaccess
file:
RewriteEngine On
RewriteRule ^(directory)/([^/.]+)/?$ /$1/index.php?id=$2 [L,NC,QSA]
确保目录
中没有其他.htaccess
文件.
这篇关于从子目录中将 URL 重写为 PHP 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!