Rewriting a URL to a PHP parameter from inside a subdirectory

主宰稳场 提交于 2019-12-17 21:10:56

问题


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.

  1. Will a 404 rewriter in the root folder overwrite any other rewriter in a subdirectory?

  2. How do I achieve what I'm after? (much like how url-shorteners work - bit.ly/shortened) from a subdirectory?


回答1:


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.



来源:https://stackoverflow.com/questions/19604954/rewriting-a-url-to-a-php-parameter-from-inside-a-subdirectory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!