从内部子目录重写URL到PHP参数 [英] Rewriting a URL to a PHP parameter from inside a subdirectory
问题描述
我在问什么的只是重写任何URL所赐子directiories到PHP的URL参数更好看的,用户友好的URL。
我知道这是可能使用Apache重写引擎和相关参数。 在我结束的问题是,我不希望重写即
mydomain.com/parameter ----------->
mydomain.com/index.php?id=parameter
不过,重写
mydomain.com/subdirectory/parameter ----------->
mydomain.com/subdirectory/index.php?id=parameter
在我的根文件夹中我有404错误,这样,重定向不存在的页面,主要的一个.htaccess文件。在我所做的测试中,我已停用这些以为自己可以覆盖其他.htaccess文件。 我想...
RewriteEngine叙述上
重写规则^目录/([^/\.]+)/?$的index.php?编号= $ 1 [L]
...并类似于一个其他片段。
被放置在根/目录下的文件夹包含此code .htaccess文件。 我想达到与被重定向根/目录/串根/目录/的index.php?ID =字符串。
由于我的.htaccess和重写的知识显然是有限的,我需要回答两个问题。
-
将404重写在根文件夹覆盖任何其他改写在子目录?
-
我如何实现我后? (很像如何网址shorteners工作 - bit.ly/shortened~~V)?从一个子目录
这个规则应该在你的 DOCUMENT_ROOT /的.htaccess
文件为你工作:
RewriteEngine叙述上
重写规则^(目录)/([^ /。] +)/?$ /$1/index.php?id=$2 [L,NC QSA]
请确保没有在目录中没有其他
。的.htaccess
文件
What I'm asking for is simply rewriting any URL-given sub-directiories to a PHP URL parameter for nicer looking, user friendly URL.
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
But rewriting
mydomain.com/subdirectory/parameter ----------->
mydomain.com/subdirectory/index.php?id=parameter
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.
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.
Since my knowledge of .htaccess and rewriting is obviously limited, I need answers to two questions.
Will a 404 rewriter in the root folder overwrite any other rewriter in a subdirectory?
How do I achieve what I'm after? (much like how url-shorteners work - bit.ly/shortened) from a subdirectory?
This rule should work for you in your DOCUMENT_ROOT/.htaccess
file:
RewriteEngine On
RewriteRule ^(directory)/([^/.]+)/?$ /$1/index.php?id=$2 [L,NC,QSA]
Make sure there is no other .htaccess
file in directory
.
这篇关于从内部子目录重写URL到PHP参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!