Welcome to OGeek Q&A 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...