# Rewrite all directory URLs with certain exceptions

**URL:** <https://www.ruby-forum.com/t/rewrite-all-directory-urls-with-certain-exceptions/224840>\
**Category:** NGINX\
**Created:** [November 28, 2012, 9:05am UTC](https://www.ruby-forum.com/t/rewrite-all-directory-urls-with-certain-exceptions/224840 "2012-11-28T09:05:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [November 28, 2012, 9:05am UTC](https://www.ruby-forum.com/t/rewrite-all-directory-urls-with-certain-exceptions/224840/1 "2012-11-28T09:05:45Z")

</div>

Hello

I have urls like [example.com/123](http://example.com/123)  
I need them rewritten to [Example Domain](http://example.com/?v=123)

However, I want to skip certain directories from rewrite  
eg.  
[example.com/status](http://example.com/status)  
[example.com/admin](http://example.com/admin)

right now I have this:

if (!-e $request\_filename){  
rewrite ^/([A-Za-z0-9-]+)/?$  
[http://www.example.com/v.php?dl=$1redirect](http://www.example.com/v.php?dl=$1redirect);  
}

Problem is that all urls are rewritten… how do I put some exceptions?

Thanks for any suggestion

---

<div class="post-metadata">

**Author:** ![cris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@cris](https://www.ruby-forum.com/u/cris)\
**Post date:** [November 28, 2012, 9:07am UTC](https://www.ruby-forum.com/t/rewrite-all-directory-urls-with-certain-exceptions/224840/2 "2012-11-28T09:07:30Z")

</div>

On Wed, Nov 28, 2012 at 3:04 PM, Cristian R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> right now I have this:
> 
> if (!-e $request\_filename){  
> rewrite ^/([A-Za-z0-9-]+)/?$ [http://www.example.com/v.php?dl=$1](http://www.example.com/v.php?dl=$1)  
> redirect;  
> }
> 
> Problem is that all urls are rewritten.. how do I put some exceptions?

put the rewrite in a location block (e.g. inside location / { }).

---

<div class="post-metadata">

**Author:** ![cris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@cris](https://www.ruby-forum.com/u/cris)\
**Post date:** [November 28, 2012, 9:31am UTC](https://www.ruby-forum.com/t/rewrite-all-directory-urls-with-certain-exceptions/224840/3 "2012-11-28T09:31:28Z")

</div>

Hello!

On Wed, Nov 28, 2012 at 10:04:58AM +0200, Cristian R. wrote:

> right now I have this:
> 
> if (!-e $request\_filename){  
> rewrite ^/([A-Za-z0-9-]+)/?$ [http://www.example.com/v.php?dl=$1redirect](http://www.example.com/v.php?dl=$1redirect);  
> }
> 
> Problem is that all urls are rewritten.. how do I put some exceptions?

I would recommend using location matching to differentiate URIs  
which should be handled differently. E.g.

```
location / {
    # you may want to use try_files here instead
    if (...) {
        rewrite ...
    }
    ...
}

location /status {
    ...
}

location /admin {
    ...
}

```

See [Module ngx\_http\_core\_module](http://nginx.org/r/location) for more information.

–  
Maxim D.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

---

<div class="post-metadata">

**Author:** ![cris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@cris](https://www.ruby-forum.com/u/cris)\
**Post date:** [November 28, 2012, 1:26pm UTC](https://www.ruby-forum.com/t/rewrite-all-directory-urls-with-certain-exceptions/224840/4 "2012-11-28T13:26:30Z")

</div>

On Wed, Nov 28, 2012 at 7:11 PM, Cristian R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Please help, server is down ☹

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#variables)**
>
> This directive appeared in version 1.11.8.

Maybe something like this.

location = / {  
if ($arg\_v) {  
rewrite ^ /v.php?dl=$arg\_v;  
}  
}

---

<div class="post-metadata">

**Author:** ![cris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@cris](https://www.ruby-forum.com/u/cris)\
**Post date:** [November 28, 2012, 1:11pm UTC](https://www.ruby-forum.com/t/rewrite-all-directory-urls-with-certain-exceptions/224840/5 "2012-11-28T13:11:54Z")

</div>

Excellent, it worked

I have another urgent matter on a server live we just switched to

I need that links like this:  
[http://www.example.com/?v=JYH253CT](http://www.example.com/?v=JYH253CT)

to be rewritten as [http://www.example.com/v.php?dl=JYH253CT](http://www.example.com/v.php?dl=JYH253CT)

Please help, server is down ☹

* * *

Cristian R.  
Web Developement & Electronic Publishing

======  
[Crilance.com](http://Crilance.com)

> **[Crilance Media Social Network](http://crilance.blogspot.com)**
>
> Video, audio, photo sharing platform written in php, ajax, mysql

---

<div class="post-metadata">

**Author:** ![cris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@cris](https://www.ruby-forum.com/u/cris)\
**Post date:** [November 28, 2012, 1:34pm UTC](https://www.ruby-forum.com/t/rewrite-all-directory-urls-with-certain-exceptions/224840/6 "2012-11-28T13:34:07Z")

</div>

On Wed, Nov 28, 2012 at 2:25 PM, Edho A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> }  
> }

I tried this from a htaccess to nginx converter

if ($query\_string ~ “^v=(.\*)$”){  
rewrite ^/index.php$ /v.php?dl=$1 break;  
}

But the resulting url is /v.php?dl=%1&v=FER34S

beats me!!

---

<div class="post-metadata">

**Author:** ![cris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@cris](https://www.ruby-forum.com/u/cris)\
**Post date:** [November 28, 2012, 1:59pm UTC](https://www.ruby-forum.com/t/rewrite-all-directory-urls-with-certain-exceptions/224840/7 "2012-11-28T13:59:15Z")

</div>

> > > to be rewritten as [http://www.example.com/v.php?dl=JYH253CT](http://www.example.com/v.php?dl=JYH253CT)  
> > > if ($arg\_v) {  
> > > rewrite ^ /v.php?dl=$arg\_v;  
> > > }  
> > > }
> 
> I tried this from a htaccess to nginx converter
> 
> if ($query\_string ~ “^v=(.\*)$”){  
> rewrite ^/index.php$ /v.php?dl=$1 break;  
> }

Try:

if ($arg\_v) {  
rewrite ^ /v.php?dl=$arg\_v break;  
}

–appa

---

<div class="post-metadata">

**Author:** ![cris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@cris](https://www.ruby-forum.com/u/cris)\
**Post date:** [November 28, 2012, 2:04pm UTC](https://www.ruby-forum.com/t/rewrite-all-directory-urls-with-certain-exceptions/224840/8 "2012-11-28T14:04:30Z")

</div>

Yes, this worked, I was also trying to get an idea of that matching in  
my  
attempt…

Why is $1 not matching right?

* * *

Cristian R.  
Web Developement & Electronic Publishing

======  
[Crilance.com](http://Crilance.com)

> **[Crilance Media Social Network](http://crilance.blogspot.com)**
>
> Video, audio, photo sharing platform written in php, ajax, mysql
