homepage Welcome to WebmasterWorld Guest from 54.196.63.93
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

This 113 message thread spans 4 pages: 113 ( [1] 2 3 4 > >     
Trailing slash
qimqim



 
Msg#: 4647301 posted 11:47 pm on Feb 20, 2014 (gmt 0)

Very big, small problem... which I can't solve.

I have a login procedure in my site. from some of the pages after a successful login the visitor is redirected to the page he was viewing. All works fine except that when the page opens it has a slash at the end and the page does not sisplay properly until manuall you delete the slasj from the url

How can I do this in htaccess?

I tried RewriteRule ^(.*)/+$ $1 [R=301,L]
but got this:
http://example.net/home3/pintotou/public_html/Asia/Indonesia/bali.php

instead of this, which is what I want:

http://example.net/Asia/Indonesia/bali.php

without the htaccess code I am getting

http://example.net/Asia/Indonesia/bali.php


Thank you for your help

[edited by: phranque at 9:30 am (utc) on Feb 21, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

 

qimqim



 
Msg#: 4647301 posted 11:50 pm on Feb 20, 2014 (gmt 0)

Sorry for the repeat; could not see where to edit

Very big, small problem... which I can't solve.

I have a login procedure in my site. from some of the pages after a successful login the visitor is redirected to the page he was viewing. All works fine except that when the page opens it has a slash at the end and the page does not sisplay properly until manuall you delete the slasj from the url

How can I do this in htaccess?

I tried
RewriteRule ^(.*)/+$ $1 [R=301,L]
but got this:
http://example.net/home3/pintotou/public_html/Asia/Indonesia/bali.php



instead of this, which is what I want:

http://example.net/Asia/Indonesia/bali.php


without the htaccess code I am getting

http://example.net/Asia/Indonesia/bali.php/



Thank you for your help

[edited by: phranque at 9:31 am (utc) on Feb 21, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4647301 posted 8:27 am on Feb 21, 2014 (gmt 0)

http://example.net/home3/pintotou/public_html/Asia/Indonesia/bali.php

That's a physical filepath. There are a couple of different ways this can get into an URL. One way is if the RewriteRule is inside an envelope such as FilesMatch; another is if the overall rules are in the wrong order, so this external redirect comes after an internal rewrite.

without the htaccess code I am getting

http://example.net/Asia/Indonesia/bali.php/

OK, good, that means the error doesn't come from your php script. At least not the filepath part of it; are you sure the php isn't adding the slash? You really don't want a situation where your own page always sends people to a redirect.

Better not take this any further until you've fine-tooth-combed your htaccess to make sure all rules are in the proper order.

Incidentally, the rule should say

RewriteRule ^(.+?)/+$ http://www.example.com/$1 [R=301,L]

Even then, the +? locution may or may not work to intercept multiple slashes, so let's hope there is only one. And how do you prevent the rule from kicking in when the target URL really is a directory and ought to keep its final slash?

[edited by: phranque at 9:33 am (utc) on Feb 21, 2014]
[edit reason] exemplified quote [/edit]

qimqim



 
Msg#: 4647301 posted 8:45 am on Feb 21, 2014 (gmt 0)

Hi. Many thnsk for your comments.

The slash was there before I started messing about with the htaccess which I know nothing about. Unfortubately I code in html/css and php is another mystery.

Could you have a look at what i have in my htaccess, please, and tell me if it makes sense, throughout.

# Use PHP5.4 Single php.ini as default
AddHandler application/x-httpd-php54s .php

Options -Indexes
AddType text/x-component .htc

# allow all except those indicated here
<Files *>
order allow,deny
allow from all

</Files>

# block visitors referred from indicated domains
RewriteEngine on
# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymlinks
Options +SymLinksIfOwnerMatch
RewriteCond %{HTTP_REFERER} semalt\.com [NC]
RewriteRule .* - [F]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F]
RewriteCond %{HTTP_HOST} ^173\.254\.28\.21
RewriteRule (.*)$ http://www.example.net/$1 [R=301]
RewriteCond %{QUERY_STRING} ^(.*)=http [NC]
RewriteRule ^(.*)$ – [F,L]

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.net$ [NC]
RewriteRule ^(.*)$ http://example.net/$1 [R=301]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.example.net/$1 [R=301]

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.net [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} ^http://.*$
RewriteRule \.(jpe?g|gif|bmp|png)$ - [F]

Redirect 301 /Pinto/oldindex.html http://example.net/Pinto/oldindex.php

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^\.localhost$ [NC]
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]

[edited by: phranque at 9:31 am (utc) on Feb 21, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

qimqim



 
Msg#: 4647301 posted 9:08 am on Feb 21, 2014 (gmt 0)

Hi

Sorry. the original htaccess code in my first post has been changed...

I tried what you suggested

RewriteRule ^(.+?)/+$ http://example.net/Asia/Indonesia/bali.php/$1 [R=301,L]

but it send me to another file: login.php instead of the page I was on

What I need is code that substitues whatever url (internal)that has a slash for the same url without the slash.

I redirecting to pages within the site.

qimqim



 
Msg#: 4647301 posted 9:27 am on Feb 21, 2014 (gmt 0)

Hi

many thanks

I've cracked it! You were right. I played around with the php and, hey presto, the slash has gone!

many thanks

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4647301 posted 9:34 am on Feb 21, 2014 (gmt 0)

welcome to WebmasterWorld, qimqim!

qimqim



 
Msg#: 4647301 posted 9:36 am on Feb 21, 2014 (gmt 0)

False alarm!

There are several pages in the site. if I went from the index page, all was fine, I was returned to the index page after logging in. If I went from another page I was getting this slash. Now I edited this leine in the final php script

header("location: ".$_SESSION['current'] . "/");

I took the lasy«t . "/" away and it worked for the page where I was getting a slash. but the front page that worked well now redirects to an unexpected page.

qimqim



 
Msg#: 4647301 posted 10:03 am on Feb 21, 2014 (gmt 0)

Firefox are throwing up a warning that

"Firefox has detected that the server is redirecting the request for this address in a way that will never complete"

Could that be htaccess? Could you have a look at what I posted above, please?

My php line now is

header("location: ".$_SESSION['current'])."";

and the expect page, the index page should http://example.com

[edited by: incrediBILL at 1:29 am (utc) on Feb 23, 2014]
[edit reason] example.com, again... [/edit]

qimqim



 
Msg#: 4647301 posted 10:03 am on Feb 21, 2014 (gmt 0)

Firefox are throwing up a warning that

"Firefox has detected that the server is redirecting the request for this address in a way that will nevr complete"

Could that be htaccess? Could you have a look at what I posted above, please?

My php line now is

header("location: ".$_SESSION['current'])."";

and the expecte page, the index page should http://example.net

[edited by: phranque at 3:28 pm (utc) on Feb 21, 2014]
[edit reason] exemplified domain [/edit]

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4647301 posted 11:10 am on Feb 21, 2014 (gmt 0)

header("location: ".$_SESSION['current'] . "/");

What the bleep? I speak about four words of php,* but surely that's an explicit and deliberate adding of a slash? What was this line intended to do?

Firefox has detected that the server is redirecting the request for this address in a way that will never complete

Urk. The "will never complete" part is not always literally true: browsers simply count the redirects resulting from a single request, and cut out at 30 or so. But here it probably really is going around in circles: from without slash to with slash to without slash to et cetera.

Your htaccess is a bit of a chaos. Crystal ball suggests that it's spliced together from at least four different sources-- hence the multiple occurrences of "RewriteEngine on". (The business about FollowSymLinks is not important, though. If a host allows htaccess, they will have turned on this option in config anyway, and it's inherited. So you normally don't need to say anything about SymLinks.) In particular, that line

Redirect 301 /Pinto/oldindex.html http://example.net/Pinto/oldindex.php

in the middle needs to be recast as a RewriteRule.

:: shuffling papers ::

Here's the boilerplate [webmasterworld.com] (scroll down to msg 4637031 with the very long quote)

and the expected page, the index page should http://example.net

That's exactly why the rule has to say
^.+
rather than
^.*
You have to exclude the front page, because that always has a slash at the end. And what about other URLs that happen to end in an intentional slash? You can't just wantonly remove all slashes. In fact you ought to have something like

RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteRule \.html/ http://www.example.net/%1 [R=301,L]


replacing "html" with any extensions you actually do use.

But won't everything work as intended if you just remove the spurious final / from the php? Since this is happening on the fly, you don't even need to keep the rule once the cause has been removed.


* It used to be three, but I finally forced myself to fix my Contact page.

qimqim



 
Msg#: 4647301 posted 11:33 am on Feb 21, 2014 (gmt 0)

Hi lucy24

Many thanks. (California? You work late...)

First, yes it's a bit of a mess and between posts I keep changing things. The original php code did not have the "/", it was just "", and that's what it is now, and it seems to be working fine for the country pages, but NOT for the front index page.

Your comment that I "have to exclude the front page, because that always has a slash at the end" leads me to think that I must pur some htaccess code to do this, but I don't know how...

I got confused with the two sets of code you menationed:

RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteRule \.html/ http://www.example.net/%1 [R=301,L]

and

That's exactly why the rule has to say
^.+
rather than
^.*


which seem to be unrelated.


As for the current .htaccees file I take it that I should eliminate the FollowSymLinks line. That incidentally appeared as if by magic, and I assume it is an automatic update by the server.

I'm glad you mentioned the 4 occurrances of "RewriteEngine on" because I was unsure if they were needed, So I am going to delete them but I wonder about what to do with the [L], I understood that you only put it on the last line. But is it the last line of each condition, or or to close the rewriteEngine on"?

The redirects 301 wre needed because I changed some file.html to file.php. How long do the redirects need to stay there for Google purposes?

many thanks lucy (go to bed... I can wait til you get up!)

qimqim



 
Msg#: 4647301 posted 12:05 pm on Feb 21, 2014 (gmt 0)

is this better now?


I've added comments. I hope they're allowed. there is a part which I cannot remember what it there for..

Help...

qimqim



 
Msg#: 4647301 posted 12:07 pm on Feb 21, 2014 (gmt 0)

The htaccess file that I had pasted disappeared...

Are you now allowed to post it?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4647301 posted 8:31 pm on Feb 21, 2014 (gmt 0)

Make sure you convert all Redirect directives to RewriteRule syntax.

The code needs some cleanup of syntax and rule order.

Add a blank line after each RewriteRule so that the code is easier to read.

Add a comment to each ruleset. In fact, number each ruleset so that it is easy to make comments about each one here.

Every Rule needs the [L] flag. It means stop here if this rule matched.

Make sure you escape all literal periods in patterns.

When a Rule is configured as a redirect the rule target should begin with protocol and hostname.

Redirects should be left in place almost forever.

qimqim



 
Msg#: 4647301 posted 8:43 pm on Feb 21, 2014 (gmt 0)

Hi, Many thanks

I am not sutre whatthe one commented (#) "No idea..." is for, and if it is correct.

I hope the forum allows the file this time

# Use PHP5.4 Single php.ini as default
AddHandler application/x-httpd-php54s .php

Options -Indexes
AddType text/x-component .htc

# allow all except those indicated here
<Files *>
order allow,deny
allow from all

</Files>

# block visitors referred from indicated domains
RewriteEngine on
# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymlinks
Options +SymLinksIfOwnerMatch
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]

#no idea what this is for. it has my ip address
RewriteCond %{HTTP_HOST} ^173\.254\.28\.21
RewriteRule (.*)$ http://example.net/$1 [R=301,L]

RewriteCond %{QUERY_STRING} ^(.*)=http [NC]
RewriteRule ^(.*)$ – [F,L]

#redirect from www
RewriteCond %{HTTP_HOST} ^www.example\.net$ [NC]
RewriteRule ^(.*)$ http://example.net/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://example.net/$1 [R=301,L]

Redirect 301 /Pinto/oldindex.html http://example.net/Pinto/oldindex.php
Redirect 301 /Asia/Indonesia/bali.html http://example.net/Asia/Indonesia/bali.php
Redirect 301 /Asia/Indonesia/indonesia.html http://example.net/Asia/Indonesia/indonesia.php

#bandwidth theft
RewriteCond %{HTTP_REFERER} !^http://example.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.net$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.net$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ - [F,NC,L]

[edited by: phranque at 9:39 am (utc) on Feb 22, 2014]
[edit reason] PLEASE USE EXAMPLE.COM [webmasterworld.com] [/edit]

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4647301 posted 9:38 pm on Feb 21, 2014 (gmt 0)

Please use example.com (or .net) to prevent auto-linking.

#no idea what this is for. it has my ip address
RewriteCond %{HTTP_HOST} ^173\.254\.28\.21
RewriteRule (.*)$ http://example.net/$1 [R=301,L]

If that rule is from the same domain's htaccess, it will create an infinite loop: "If the IP is me, redirect to example.net even if that's already what I requested".

Is it your own IP address or the person you got the code from?

[edited by: phranque at 9:38 am (utc) on Feb 22, 2014]
[edit reason] exemplified quote [/edit]

qimqim



 
Msg#: 4647301 posted 5:36 am on Feb 22, 2014 (gmt 0)

Hi Lucy

It id my own SHARED IP. In fact, if you place it in the address bar you end up in a JustHost page. I guess the idea was that to be SEO friendly whatever address you used it should resolve in the same style, i.e. no www.

Shall I delete it?

What about what comes next which I believe was related to the same?

RewriteCond %{QUERY_STRING} ^(.*)=http [NC]
RewriteRule ^(.*)$ – [F,L]

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4647301 posted 6:10 am on Feb 22, 2014 (gmt 0)

query string = http-something? That sounds like a site-specific rule. Wouldn't it also kill all visits from search engines? They come with an enormous long query string that includes the full name of your page, beginning in http://

:: detour to check ::

Oh, right, everything is escaped, so it ends up as http%3A%2F%2F et cetera. But the http is there, loud and clear.

Incidentally, the form
^(.*)
is never necessary unless you're capturing-- and when you are capturing, there are almost certainly more efficient forms. When you're matching without a capture, simply leave off everything before the part you need to match.

Edit:
It id my own SHARED IP.

Oh, oops, your site's IP. I thought you meant your own-- the one you use when signing on. I also now realize that I misread {HTTP_HOST} (the site) as {REMOTE_HOST} (the visitor). World of difference!

That being the case, the rule won't do any harm. But unless you're the only domain on the server, it also won't do any good, because requests for the raw IP address will just get the generic server page that you found. Nobody will reach your specific domain unless they ask for it by name. So you may as well delete the rule simply to save the server some work.

Besides, the domain-name-canonicalization redirect already takes care of the numerical-IP issue. You've got one of those, haven't you?

qimqim



 
Msg#: 4647301 posted 6:18 am on Feb 22, 2014 (gmt 0)

So, your advice is to get rid of these two, right?

#no idea what this is for. it has my ip address
RewriteCond %{HTTP_HOST} ^173\.254\.28\.21
RewriteRule (.*)$ http://example.net/$1 [R=301,L]

RewriteCond %{QUERY_STRING} ^(.*)=http [NC]
RewriteRule ^(.*)$ – [F,L]

many thanks

[edited by: phranque at 9:36 am (utc) on Feb 22, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4647301 posted 9:24 am on Feb 22, 2014 (gmt 0)

Yes. The first rule is pointless; it just takes up server resources. The second one would deny all visitors from search engines. I have to assume you don't want that!

Incidentally, the [F] flag carries an implied [L]. So does [G], although [R] (of any kind) does not. But [L] doesn't mean "Show them the door and make no detours and no exceptions". It just means "We're done with mod_rewrite for now".

qimqim



 
Msg#: 4647301 posted 9:36 am on Feb 22, 2014 (gmt 0)

Thank you very much Lucy

Over and out (for now...)

PS: must be getting late in Eureka, Califirnia...

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4647301 posted 9:42 am on Feb 22, 2014 (gmt 0)

IMPORTANT: Please Use Example.com For Domain Names in Posts [webmasterworld.com]

or example.net if you prefer.

qimqim



 
Msg#: 4647301 posted 9:48 am on Feb 22, 2014 (gmt 0)

Ok...

Sorry!

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4647301 posted 10:27 am on Feb 23, 2014 (gmt 0)

Make sure you convert all Redirect directives to RewriteRule syntax.

The code needs some cleanup of syntax and rule order.

Add a blank line after each RewriteRule so that the code is easier to read.

Number each ruleset so that it is easy to make comments about each one here.

Every Rule needs the [L] flag. It means stop here if this rule matched.

Make sure you escape all literal periods in patterns.

[F,L] should be [F] and these rules should be first.

This rule blocks certain types of malicious requests
RewriteCond %{QUERY_STRING} http [NC]
RewriteRule .* – [F]

[edited by: phranque at 10:43 pm (utc) on Feb 23, 2014]
[edit reason] fixed typo [/edit]

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4647301 posted 10:38 am on Feb 23, 2014 (gmt 0)

This rule blocks certain types of malicious requests

Pause here for "D'oh!" moment as I realize that search engines have nothing to do with it, because that's the referer's query string, not your own.

Uhm. I think that's two separate severe errors in a single thread. Or two severe errors in two different threads within a short time. Maybe I should just go to bed. Yes, qimqim, before dawn :-P

qimqim



 
Msg#: 4647301 posted 10:48 am on Feb 23, 2014 (gmt 0)

Now, you've both got me very confused...

especially as I submitted another sitemap.xml to Google 2/3 days ago and this morning out of 168 files they only had indexed 2 (well, just now it is 8) and I assumed I had blocked them.

So, Do I put the code back in? And what does it do?


I sent another message few mins ago, but it disappeared...

qimqim



 
Msg#: 4647301 posted 10:54 am on Feb 23, 2014 (gmt 0)

I could not understand if by this: "Make sure you escape all literal periods in patterns." you mean there is one missing.

Other than that and the numbering is this ok now:?



# Use PHP5.4 Single php.ini as default
AddHandler application/x-httpd-php54s .php

Options -Indexes
AddType text/x-component .htc

# allow all except those indicated here
<Files *>
order allow,deny
allow from all

</Files>

# block visitors referred from indicated domains
RewriteEngine on
# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymlinks

Options +SymLinksIfOwnerMatch
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [L]

#bandwidth theft
RewriteCond %{HTTP_REFERER} !^http://example.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.net$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.net$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ - [L,NC]

#redirect from www
RewriteCond %{HTTP_HOST} ^www.example\.net$ [NC]
RewriteRule ^(.*)$ http://example.net/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://example.net/$1 [R=301,L]

Redirect 301 /Pinto/oldindex.html http://example.net/Pinto/oldindex.php
Redirect 301 /Asia/Indonesia/bali.html http://example.net/Asia/Indonesia/bali.php
Redirect 301 /Asia/Indonesia/indonesia.html http://example.net/Asia/Indonesia/indonesia.php

[edited by: phranque at 11:21 am (utc) on Feb 23, 2014]
[edit reason] example.net [/edit]

qimqim



 
Msg#: 4647301 posted 10:56 am on Feb 23, 2014 (gmt 0)

...and should I put this back in?

This rule blocks certain types of malicious requests
RewriteCond %{QUERY_STRING} http [NC]
RewriteRule .* – [F]

Could you translate it into English (or American, if you prefer...), please?

qimqim



 
Msg#: 4647301 posted 10:57 am on Feb 23, 2014 (gmt 0)

This rule blocks certain types of malicious requests
RewriteCond %{QUERY_STRING} http [NC]
RewriteRule .* – [F]


Hi

Lucy told me that it also blocks bots like Google, so I took it out.

I understood from above that with [F] you don't need [L]. I guess now that it is the other way round, right?

This 113 message thread spans 4 pages: 113 ( [1] 2 3 4 > >
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved