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
1.1k views
in Technique[技术] by (71.8m points)

regex - "Two Way" Rewrite

So, I'm trying to accomplish this:

We have a URL /brand/new-inventory.php. This is a physical file within our site. What I'd like it to be is /brand/new. Now that was easy. All I had to add was RewriteRule ^brand/new$ brand/new-inventory.php [L,QSA].

Now, what I'd like to get setup and I've been struggling with is this:

We need /brand/new-inventory.php when viewed by someone to redirect to /brand/new so we have consistent URL's and what-not.

But, when I try RewriteRule ^brand/new-inventory.php$ brand/new [R=301,L] Firefox starts to complain about a redirect loop that will never end.

How can I rewrite a page to respond to a different URL, and redirect the old URL to the new one?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Have it this way:

RewriteCond %{THE_REQUEST} s/+brand/new-inventory.php[/s?] [NC]
RewriteRule ^ /brand/new [R=302,L]

RewriteRule ^brand/new$ /brand/new-inventory.php [L,NC]

Difference is use of %{THE_REQUEST} that represents original request received by Apache from your browser. Value of this variable doesn't change with application of other rewrite rules.


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

...