Forum Moderators: phranque

Message Too Old, No Replies

How to add a trailing slash at end of url if user misses it

URL rewrite

         

satheeshkumar

2:04 pm on May 23, 2011 (gmt 0)

10+ Year Member



Hi,

I am having a https site like [mysite.com...] and having many directories in it like [mysite.com...] , etc.

When i miss the trailing slash at the end of docs (https://mysite.com/docs), it is redirecting to http as (http://mysite.com/docs/)

my rewrite rule is as below:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) [%{HTTP_HOST}%{REQUEST_URI}...]

Can you please suggest me how can i modify this, so that it can be redirected to (http://mysite.com/docs/) instead of http. I want to add a "/" at the end of URL if the user misses it.

Kindly help me on this.

phpJoeMo

7:09 pm on May 26, 2011 (gmt 0)

10+ Year Member



This should do the trick:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) [mydomain.com...] [R]

Try it and let me know if it works

g1smd

7:34 pm on May 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That code will result in an 302 redirect.

If you're not careful you could end up with an infinite redirect loop.

You will need to test for "docs" and "NOT docs" to avoid this.

phpJoeMo

7:40 pm on May 26, 2011 (gmt 0)

10+ Year Member



How can that be?

Once the redirect happens the Rewrite Condition doesn't match because the request is now in https, so no further rewriting takes place.

g1smd

7:46 pm on May 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You should have two rules, mirror images of each other.

If URL begins with "this" redirect http to https.

If URL does NOT begin with "this" redirect https to http.

phpJoeMo

8:04 pm on May 26, 2011 (gmt 0)

10+ Year Member



In his situation though I think that's unnecessary.

It appears that he is only trying to force an https connection.

Once that's established, there's no need for further processing based on whether or not https is enabled.

satheeshkumar

1:11 pm on May 27, 2011 (gmt 0)

10+ Year Member



Hi,

I tried the parameter ProxyPreserveHost set to off from On. Then my URL redirection issue is resolved. Any idea about this?

satheeshkumar

3:16 pm on Aug 17, 2011 (gmt 0)

10+ Year Member



Hi,

The issue is again whin i am using a load balancer(LB) in front of apache..

I am using 8080 virtualhost without SSL in apache and SSL is used in LB. so LB to apache is http only through port 8080.

I kept my content in a tomcat application server, and the folder docs, and i proxypassed this folder in apache ssl.conf file. I specified a default file in tomcat as well if i access /docs.

In this case, if i miss slash at URL end, like [mysite.com,...] there is no problem as the content is in apache. Also, while accessing [mysite.com...] has no issues.

But, if i access [mysite.com...] , it ends up with [mysite.com...] result in error (http redirect). I noticed in logs there is a 302 redirect in this case.

User--(https:443)-->LB--(http:8080)-->apache--(http:8080)-->tomcat

I am unable to frame rewrire rule for apache for this case.

Please someone help.

lucy24

6:43 pm on Aug 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Backtrack. Based on your last post, there are-- sit tight-- eight possible inputs. In each case, look at what happens by default (that is, if you have no added htaccess rules), and what you want to have happen. Your desired result is the same for the second block of 4 as for the first block, but it sounds as if your server is handling some of them differently.

http://example.com
http://example.com/
[example.com...]
[example.com...] >> no change needed

http://example.com/blahblah **
http://example.com/blahblah/
[example.com...] **
[example.com...] >> no change needed

** Here you also need to determine whether blahblah is a file or directory: that is, does the part after the last slash include dot-plus-extension.

I said "input" but of course most of them won't be type-ins. So you also need to see what's arriving in your htaccess. Any site-internal links should arrive with the same protocol as their source (http or https). Conversely, check whether any changes or expansions-- such as adding index.html-- have happened before the page reaches your htaccess.

And, when it's all done, regularize to with-or-without www. Whew.

satheeshkumar

7:14 am on Aug 18, 2011 (gmt 0)

10+ Year Member



I disabled .htaccess in performance point of view and added all configs in httpd.conf and ssl.conf files.

What i noted is that when i miss / at the end, it is considering as files and not directories.

I kept my docs folder in tomcat server, and if i request that without slash at the end, it is searching for file named docs in tomcat. I noticed a 302 redirect happened in apache before reaching tomcat server. (I noticed a 302 redirect in apache and then the external redirect ends as [mysite.com...] in browser. But again i noticed in apache logs, it is picking correct proxypass directive and sending request to tomcat server for the docs, but nothing is recorded in tomcat logs..)

Though user hits https, since apache is reaching request as http (because ssl is in LB and LB to apache is http). The external 302 redirect adds a slash at the end and calls with http. so this external redirect results in error, because external redirect needs to be https. Also i am using multiple CNAMES for my site and so i dont need to hardcode my URL in rewrite rule.

I tried couple of suggestions above and none seems working.

g1smd

7:53 am on Aug 18, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What i noted is that when i miss / at the end, it is considering as files and not directories.
Correct! The HTTP specs say that:
example.com/this
- is the URL for a page and
example.com/this/
- is the URL for a folder or for the index page in a folder.

The
DirectorySlash
directive may be useful here.

satheeshkumar

8:06 am on Aug 18, 2011 (gmt 0)

10+ Year Member



But coming to my case, it is perfectly doing a 302 redirect with / added at end, but with http.(because browser ends up with http). shall you suggest a rewrire rule for my case (i dont need to hardcode my URL in rewrite rule)

satheeshkumar

2:57 pm on Aug 19, 2011 (gmt 0)

10+ Year Member



Hi, I have a issue that when i tried to write rewrite rules in httpd.conf or ssl.conf files, it is not working. the only place it is working is .htaccess files.

I need to rewrite the URLs for which the actual content is in separate tomcat server (so i will write proxy pass in ssl.conf for those directories). Since the content is not in any of apache docroot, i will not be able to write rules in htaccess files in apache docroot.

Please suggest any ideas...