# Trailing Slash redirection poblem

**URL:** <https://www.ruby-forum.com/t/trailing-slash-redirection-poblem/244546>\
**Category:** NGINX\
**Created:** [April 30, 2016, 9:47am UTC](https://www.ruby-forum.com/t/trailing-slash-redirection-poblem/244546 "2016-04-30T09:47:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [April 30, 2016, 9:47am UTC](https://www.ruby-forum.com/t/trailing-slash-redirection-poblem/244546/1 "2016-04-30T09:47:38Z")

</div>

Dear All,  
Need some help to remove trailing Slash.  
I have been trying to remove the trailing slash with this redirection  
rule.  
rewrite ^/(.\*)/$ /$1 permanent;

however it is creating a loop.

curl -I [https://xxxx.com/live/](https://xxxx.com/live/)

HTTP/1.1 301 Moved Permanently  
Server: nginx/1.6.2  
Date: Sat, 30 Apr 2016 07:39:07 GMT  
Content-Type: text/html  
Content-Length: 184  
Location: [https://xxxx.com/live](https://xxxx.com/live)  
Connection: keep-alive

curl -I [https://xxxx.com/live](https://xxxx.com/live)

HTTP/1.1 301 Moved Permanently  
Server: nginx/1.6.2  
Date: Sat, 30 Apr 2016 07:39:29 GMT  
Content-Type: text/html; charset=UTF-8  
Connection: keep-alive  
Location: [https://xxxx.com/live/](https://xxxx.com/live/)

Can you please guide what i am doing wrong here.

Thanks,  
Yousuf

---

<div class="post-metadata">

**Author:** ![muhammad\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@muhammad\_k](https://www.ruby-forum.com/u/muhammad_k)\
**Post date:** [May 1, 2016, 10:17am UTC](https://www.ruby-forum.com/t/trailing-slash-redirection-poblem/244546/2 "2016-05-01T10:17:20Z")

</div>

> > The configuration you have shown says “if the request is for /live/,  
> > ask the browser to instead request /live”.

> > The configuration you have not shown says “if the request is for /live,  
> > ask the browser to instead request /live/”.

Thanks for the tip I think I got the problem but could not resolve it.  
try\_files $uri $uri.html $uri/ /index.php?q=$request\_uri;  
I even tried deleting “$uri.html $uri/” but still creating loop. I know  
this is a important line to display wordpress pages . now the error  
turn  
404 from redirection loop.

> > You should not have both of those in the same configuration file, or  
> > you get a loop.

Yes I got this now as shared. Thanks for the tip again.

> > The not-shown configuration is usually a very good idea if “/live”  
> > is to be served from the filesystem and corresponds to a directory.

> > So: why do you want to remove the trailing slash, in the shown  
> > configuration?  
> > Actually this is a requirement from my client.

> > If you want /live to redirect to /live/, then you should configure thing  
> > such that /live/ does not redirect to /live.

No I want /live/ to be redirected to /live and the rule which is  
redirecting /live to /live/ and ending up as loop Is also important.  
Any  
helpful advice is highly appreciated.

---

<div class="post-metadata">

**Author:** ![muhammad\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@muhammad\_k](https://www.ruby-forum.com/u/muhammad_k)\
**Post date:** [April 30, 2016, 10:40am UTC](https://www.ruby-forum.com/t/trailing-slash-redirection-poblem/244546/3 "2016-04-30T10:40:48Z")

</div>

On Sat, Apr 30, 2016 at 12:47:20PM +0500, Muhammad Yousuf K. wrote:

Hi there,

> I have been trying to remove the trailing slash with this redirection rule.  
> rewrite ^/(.\*)/$ /$1 permanent;
> 
> however it is creating a loop.
> 
> curl -I [https://xxxx.com/live/](https://xxxx.com/live/)
> 
> HTTP/1.1 301 Moved Permanently  
> Location: [https://xxxx.com/live](https://xxxx.com/live)

> curl -I [https://xxxx.com/live](https://xxxx.com/live)
> 
> HTTP/1.1 301 Moved Permanently  
> Location: [https://xxxx.com/live/](https://xxxx.com/live/)

> Can you please guide what i am doing wrong here.

The configuration you have shown says “if the request is for /live/,  
ask the browser to instead request /live”.

The configuration you have not shown says “if the request is for /live,  
ask the browser to instead request /live/”.

You should not have both of those in the same configuration file, or  
you get a loop.

The not-shown configuration is usually a very good idea if “/live”  
is to be served from the filesystem and corresponds to a directory.

So: why do you want to remove the trailing slash, in the shown  
configuration?

If you want /live to redirect to /live/, then you should configure thing  
such that /live/ does not redirect to /live.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![muhammad\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@muhammad\_k](https://www.ruby-forum.com/u/muhammad_k)\
**Post date:** [May 1, 2016, 10:19am UTC](https://www.ruby-forum.com/t/trailing-slash-redirection-poblem/244546/4 "2016-05-01T10:19:41Z")

</div>

just want to make it more clear that when i comment out this line  
#try\_files  
$uri $uri.html $uri/ /index.php?q=$request\_uri; error become 404 from  
redirection loop error.

On Sun, May 1, 2016 at 1:16 PM, Muhammad Yousuf K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![muhammad\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@muhammad\_k](https://www.ruby-forum.com/u/muhammad_k)\
**Post date:** [May 1, 2016, 4:44pm UTC](https://www.ruby-forum.com/t/trailing-slash-redirection-poblem/244546/5 "2016-05-01T16:44:38Z")

</div>

So these opposite redirection rules are fighting each other and are the  
source of your problem, which Francis helped you to alleviate.

Sit back, grab an erasable whiteboard, scratch your head and think about  
your website’s design: both rules probably have their use in their own  
corner, but you shall not have them activated at the same spot at any  
given  
time, as it will result in an infinite loop: a computer does exactly  
what  
you tell it to do. There is nothing stupider than that.  
The problem lies between the desk and the chair. ;o)

​Do not merely copy-paste pre-cooked Wordpress-hypothetically-ready  
configuration directives​. Try to recreate it by hand, understanding the  
use and intention of every line.  
nginx docs [http://nginx.org/en/docs/](http://nginx.org/en/docs/) will most probably come helpful.

## Enjoy learning! :o)

_B. R._

On Sun, May 1, 2016 at 10:18 AM, Muhammad Yousuf K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![muhammad\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@muhammad\_k](https://www.ruby-forum.com/u/muhammad_k)\
**Post date:** [May 2, 2016, 2:48pm UTC](https://www.ruby-forum.com/t/trailing-slash-redirection-poblem/244546/6 "2016-05-02T14:48:59Z")

</div>

first of all my apologies if i question something inapplicable , this  
could be the result of my less knowledge in nginx. i hope you guys don’t  
mind.

Thanks for the reply, actually we have 5 categories. and then we have  
hundred plus post in each category. now when user click on our category.  
he  
sees intro about the category and then list of all posts.  
when user click on catagory it open up like this  
[https://xxxx.com/catagory/](https://xxxx.com/catagory/)

and when post is clicked it opens up like this  
[https://xxxx.com/catagory/posts.html](https://xxxx.com/catagory/posts.html)

but now what client want is when some one open up the category it should  
open up like this.  
[https://xxxx.com/catagory](https://xxxx.com/catagory) (with out trailing slash)

This is all what they want.

Thanks,  
Yousuf

---

<div class="post-metadata">

**Author:** ![muhammad\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@muhammad\_k](https://www.ruby-forum.com/u/muhammad_k)\
**Post date:** [May 3, 2016, 9:23pm UTC](https://www.ruby-forum.com/t/trailing-slash-redirection-poblem/244546/7 "2016-05-03T21:23:16Z")

</div>

On Mon, May 02, 2016 at 05:48:12PM +0500, Muhammad Yousuf K. wrote:

Hi there,

> Thanks for the reply, actually we have 5 categories. and then we have  
> hundred plus post in each category. now when user click on our category. he  
> sees intro about the category and then list of all posts.  
> when user click on catagory it open up like this  
> [https://xxxx.com/catagory/](https://xxxx.com/catagory/)

It sounds to me like that is an internal-to-wordpress thing, so you  
may be better off removing any clever nginx config and just configure  
wordpress to create the links that you want it to create.

(I do not know if it possible to configure wordpress like that.)

> and when post is clicked it opens up like this  
> [https://xxxx.com/catagory/posts.html](https://xxxx.com/catagory/posts.html)
> 
> but now what client want is when some one open up the category it should  
> open up like this.  
> [https://xxxx.com/catagory](https://xxxx.com/catagory) (with out trailing slash)

When someone clicks on catagory, they are following a link in whatever  
html was returned from their previous request. If that link has a  
trailing  
slash, they’ll ask for the trailing slash.

If the content on the catagory page contains links like “post1.html”,  
then you will need the trailing slash – or you will need to change  
the content to be of the form “catagory/post1.html”. That should all be  
configurable within wordpress, if anywhere.

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![muhammad\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@muhammad\_k](https://www.ruby-forum.com/u/muhammad_k)\
**Post date:** [May 2, 2016, 11:18am UTC](https://www.ruby-forum.com/t/trailing-slash-redirection-poblem/244546/8 "2016-05-02T11:18:40Z")

</div>

On Sun, May 01, 2016 at 01:16:51PM +0500, Muhammad Yousuf K. wrote:

Hi there,

> > > The configuration you have shown says “if the request is for /live/,  
> > > ask the browser to instead request /live”.
> 
> > > The configuration you have not shown says “if the request is for /live,  
> > > ask the browser to instead request /live/”.
> 
> Thanks for the tip I think I got the problem but could not resolve it.

If you make the request for “/live”, what response do you actually want?

The contents of some file? The output of wordpress being given some  
arguments? Be as specific as possible, and it is much more likely that  
you will understand how nginx needs to be configured to do that.

> > > So: why do you want to remove the trailing slash, in the shown  
> > > configuration?  
> > > Actually this is a requirement from my client.

Sometimes, the correct response to a client requirement is “no”.

It is not yet clear to me whether this is one of those times.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![muhammad\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@muhammad\_k](https://www.ruby-forum.com/u/muhammad_k)\
**Post date:** [May 4, 2016, 2:53pm UTC](https://www.ruby-forum.com/t/trailing-slash-redirection-poblem/244546/9 "2016-05-04T14:53:01Z")

</div>

Thanks for the info. will check on that.
