Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

regex - Rewriting a URL to a PHP parameter from inside a subdirectory

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...