You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Dave Wreski <dw...@guardiandigital.com.INVALID> on 2024/01/16 03:15:53 UTC

[users@httpd] Removing trailing slashes with query strings

Hi,

I have the following rule that works well to remove trailing slashes 
from URLs:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]

This is done to prevent the non-slash URL from being treated as 
duplicate content with the URL with a slash.

The problem is that there are exceptions, such as when we want to add 
query strings to the end of a URL:

https://example.com/administrator/path/?cms_settings

I've also tried a variation, like:

https://example.com/administrator/path?cms_settings

but somehow the slash is replaced back into the URL and I don't know 
where it comes from.

Adding index.php to the path works properly because it's then not a 
directory:

https://example.com/administrator/path/index.php?cms_settings

Any ideas greatly appreciated.

Thanks,
Dave


Re: [users@httpd] Removing trailing slashes with query strings

Posted by Frank Gingras <th...@apache.org>.
On Thu, Jan 18, 2024 at 7:44 AM Dave Wreski
<dw...@guardiandigital.com.invalid> wrote:

> Hi,
>
> [Tue Jan 16 08:55:13.289205 2024] [rewrite:trace1] [pid 904886:tid 904938]
>>>> mod_rewrite.c(493): [client 68.111.193.42:0] 68.195.193.42 - - [
>>>> webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial
>>>> <http://webstage.example.com/sid#560ba7f01a68][rid%237f2b1c0bc210/initial>]
>>>> redirect to
>>>> https://webstage.example.com/administrator/sqlantern-joomla/php?cms_settings
>>>> [REDIRECT/301]
>>>>
>>>
>>> Based on those log entries, the rewrite worked fine.
>>>
>>> Perhaps you're dealing with DirectorySlash here.
>>>
>>> Notice the last line redirected to itself, causing an infinite loop of
>>> redirects.
>>>
>>> I also wanted to add that, given the RewriteCond rule may be working
>>> correctly, how can I change it to accommodate query strings, or this
>>> particular query string?
>>>
>>> Thanks,
>>> Dave
>>>
>>>
>>>
>> If your rules are in .htaccess, the latter file will be parsed over and
>> over until it stops matching, so use the appropriate RewriteCond to break
>> that loop, or better yet, edit your vhost instead.
>>
>> Yes, that's what I'm asking for help to do :-) This rewritecond is
>> included in my vhost config using an Include statement.
>>
>> Thanks,
>> Dave
>>
>
> What context is the include directive placed in?
>
> It's in the VirtualHost directly, not in a Directory.
>
>
>
>
>
>
The following RewriteCond won't work in the vhost context:

RewriteCond %{REQUEST_FILENAME} !-d

You need to use:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d

Re: [users@httpd] Removing trailing slashes with query strings

Posted by Dave Wreski <dw...@guardiandigital.com.INVALID>.
Hi,


>>>>             [Tue Jan 16 08:55:13.289205 2024] [rewrite:trace1] [pid
>>>>             904886:tid 904938] mod_rewrite.c(493): [client
>>>>             68.111.193.42:0 <http://68.111.193.42:0>] 68.195.193.42
>>>>             - -
>>>>             [webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial
>>>>             <http://webstage.example.com/sid#560ba7f01a68][rid%237f2b1c0bc210/initial>]
>>>>             redirect to
>>>>             https://webstage.example.com/administrator/sqlantern-joomla/php?cms_settings
>>>>             [REDIRECT/301]
>>>>
>>>>
>>>>         Based on those log entries, the rewrite worked fine.
>>>>
>>>>         Perhaps you're dealing with DirectorySlash here.
>>>
>>>         Notice the last line redirected to itself, causing an
>>>         infinite loop of redirects.
>>>
>>         I also wanted to add that, given the RewriteCond rule may be
>>         working correctly, how can I change it to accommodate query
>>         strings, or this particular query string?
>>
>>         Thanks,
>>         Dave
>>
>>
>>
>>     If your rules are in .htaccess, the latter file will be parsed
>>     over and over until it stops matching, so use the appropriate
>>     RewriteCond to break that loop, or better yet, edit your vhost
>>     instead.
>
>     Yes, that's what I'm asking for help to do :-) This rewritecond is
>     included in my vhost config using an Include statement.
>
>     Thanks,
>     Dave
>
>
> What context is the include directive placed in?

It's in the VirtualHost directly, not in a Directory.





Re: [users@httpd] Removing trailing slashes with query strings

Posted by Frank Gingras <th...@apache.org>.
On Wed, Jan 17, 2024 at 9:21 AM Dave Wreski
<dw...@guardiandigital.com.invalid> wrote:

> Hi,
>
> [Tue Jan 16 08:55:13.289205 2024] [rewrite:trace1] [pid 904886:tid 904938]
>>> mod_rewrite.c(493): [client 68.111.193.42:0] 68.195.193.42 - - [
>>> webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial
>>> <http://webstage.example.com/sid#560ba7f01a68][rid%237f2b1c0bc210/initial>]
>>> redirect to
>>> https://webstage.example.com/administrator/sqlantern-joomla/php?cms_settings
>>> [REDIRECT/301]
>>>
>>
>> Based on those log entries, the rewrite worked fine.
>>
>> Perhaps you're dealing with DirectorySlash here.
>>
>> Notice the last line redirected to itself, causing an infinite loop of
>> redirects.
>>
>> I also wanted to add that, given the RewriteCond rule may be working
>> correctly, how can I change it to accommodate query strings, or this
>> particular query string?
>>
>> Thanks,
>> Dave
>>
>>
>>
> If your rules are in .htaccess, the latter file will be parsed over and
> over until it stops matching, so use the appropriate RewriteCond to break
> that loop, or better yet, edit your vhost instead.
>
> Yes, that's what I'm asking for help to do :-) This rewritecond is
> included in my vhost config using an Include statement.
>
> Thanks,
> Dave
>

What context is the include directive placed in?

Re: [users@httpd] Removing trailing slashes with query strings

Posted by Dave Wreski <dw...@guardiandigital.com.INVALID>.
Hi,

>>>         [Tue Jan 16 08:55:13.289205 2024] [rewrite:trace1] [pid
>>>         904886:tid 904938] mod_rewrite.c(493): [client
>>>         68.111.193.42:0 <http://68.111.193.42:0>] 68.195.193.42 - -
>>>         [webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial
>>>         <http://webstage.example.com/sid#560ba7f01a68][rid%237f2b1c0bc210/initial>]
>>>         redirect to
>>>         https://webstage.example.com/administrator/sqlantern-joomla/php?cms_settings
>>>         [REDIRECT/301]
>>>
>>>
>>>     Based on those log entries, the rewrite worked fine.
>>>
>>>     Perhaps you're dealing with DirectorySlash here.
>>
>>     Notice the last line redirected to itself, causing an infinite
>>     loop of redirects.
>>
>     I also wanted to add that, given the RewriteCond rule may be
>     working correctly, how can I change it to accommodate query
>     strings, or this particular query string?
>
>     Thanks,
>     Dave
>
>
>
> If your rules are in .htaccess, the latter file will be parsed over 
> and over until it stops matching, so use the appropriate RewriteCond 
> to break that loop, or better yet, edit your vhost instead.

Yes, that's what I'm asking for help to do :-) This rewritecond is 
included in my vhost config using an Include statement.

Thanks,
Dave

Re: [users@httpd] Removing trailing slashes with query strings

Posted by Frank Gingras <th...@apache.org>.
On Tue, Jan 16, 2024 at 3:33 PM Dave Wreski
<dw...@guardiandigital.com.invalid> wrote:

> Hi,
> On 1/16/24 3:03 PM, Dave Wreski wrote:
>
>
> [Tue Jan 16 08:55:13.289205 2024] [rewrite:trace1] [pid 904886:tid 904938]
>> mod_rewrite.c(493): [client 68.111.193.42:0] 68.195.193.42 - - [
>> webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial
>> <http://webstage.example.com/sid#560ba7f01a68][rid%237f2b1c0bc210/initial>]
>> redirect to
>> https://webstage.example.com/administrator/sqlantern-joomla/php?cms_settings
>> [REDIRECT/301]
>>
>
> Based on those log entries, the rewrite worked fine.
>
> Perhaps you're dealing with DirectorySlash here.
>
> Notice the last line redirected to itself, causing an infinite loop of
> redirects.
>
> I also wanted to add that, given the RewriteCond rule may be working
> correctly, how can I change it to accommodate query strings, or this
> particular query string?
>
> Thanks,
> Dave
>
>
>
If your rules are in .htaccess, the latter file will be parsed over and
over until it stops matching, so use the appropriate RewriteCond to break
that loop, or better yet, edit your vhost instead.

DirectorySlash off won't help you if the user decides to add the "/" to the
URI and you want to remove it, for some reason. It will also break other
things.

Re: [users@httpd] Removing trailing slashes with query strings

Posted by Dave Wreski <dw...@guardiandigital.com.INVALID>.
Hi,

On 1/16/24 3:03 PM, Dave Wreski wrote:
>
>>     [Tue Jan 16 08:55:13.289205 2024] [rewrite:trace1] [pid
>>     904886:tid 904938] mod_rewrite.c(493): [client 68.111.193.42:0
>>     <http://68.111.193.42:0>] 68.195.193.42 - -
>>     [webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial
>>     <http://webstage.example.com/sid#560ba7f01a68][rid%237f2b1c0bc210/initial>]
>>     redirect to
>>     https://webstage.example.com/administrator/sqlantern-joomla/php?cms_settings
>>     [REDIRECT/301]
>>
>>
>> Based on those log entries, the rewrite worked fine.
>>
>> Perhaps you're dealing with DirectorySlash here.
>
> Notice the last line redirected to itself, causing an infinite loop of 
> redirects.
>
I also wanted to add that, given the RewriteCond rule may be working 
correctly, how can I change it to accommodate query strings, or this 
particular query string?

Thanks,
Dave


Re: [users@httpd] Removing trailing slashes with query strings

Posted by Dave Wreski <dw...@guardiandigital.com.INVALID>.
>     [Tue Jan 16 08:55:13.289205 2024] [rewrite:trace1] [pid 904886:tid
>     904938] mod_rewrite.c(493): [client 68.111.193.42:0
>     <http://68.111.193.42:0>] 68.195.193.42 - -
>     [webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial
>     <http://webstage.example.com/sid#560ba7f01a68][rid%237f2b1c0bc210/initial>]
>     redirect to
>     https://webstage.example.com/administrator/sqlantern-joomla/php?cms_settings
>     [REDIRECT/301]
>
>
> Based on those log entries, the rewrite worked fine.
>
> Perhaps you're dealing with DirectorySlash here.

Notice the last line redirected to itself, causing an infinite loop of 
redirects.

I don't think I'm involving the use of DirectorySlash or mod_dir, but 
that looks interesting. Perhaps I should be considering using that to 
strip off the trailing slash?


    DirectorySlashDirective

Description: 
<https://httpd.apache.org/docs/2.4/mod/directive-dict.html#Description> 
Toggle trailing slash redirects on or off
Syntax: 
<https://httpd.apache.org/docs/2.4/mod/directive-dict.html#Syntax> 
|DirectorySlash On|Off|
Default: 
<https://httpd.apache.org/docs/2.4/mod/directive-dict.html#Default> 
|DirectorySlash On|
Context: 
<https://httpd.apache.org/docs/2.4/mod/directive-dict.html#Context> 
server config, virtual host, directory, .htaccess
Override: 
<https://httpd.apache.org/docs/2.4/mod/directive-dict.html#Override> 
Indexes
Status: 
<https://httpd.apache.org/docs/2.4/mod/directive-dict.html#Status> 	Base
Module: 
<https://httpd.apache.org/docs/2.4/mod/directive-dict.html#Module> 	mod_dir

Thanks,
Dave



Re: [users@httpd] Removing trailing slashes with query strings

Posted by Frank Gingras <th...@apache.org>.
On Tue, Jan 16, 2024 at 9:30 AM Dave Wreski
<dw...@guardiandigital.com.invalid> wrote:

> Hi,
>
> I have the following rule that works well to remove trailing slashes from
>> URLs:
>>
>> RewriteCond %{REQUEST_FILENAME} !-d
>> RewriteRule ^(.*)/$ $1 [R=301,L]
>>
>> This is done to prevent the non-slash URL from being treated as duplicate
>> content with the URL with a slash.
>>
>> The problem is that there are exceptions, such as when we want to add
>> query strings to the end of a URL:
>>
>> https://example.com/administrator/path/?cms_settings
>>
>> I've also tried a variation, like:
>>
>> https://example.com/administrator/path?cms_settings
>>
>> but somehow the slash is replaced back into the URL and I don't know
>> where it comes from.
>>
>> Adding index.php to the path works properly because it's then not a
>> directory:
>>
>> https://example.com/administrator/path/index.php?cms_settings
>>
>> Any ideas greatly appreciated.
>>
>> Thanks,
>> Dave
>>
>>
>>
> What does the rewrite log say, exactly?
>
> There's a tremendous amount of noise at trace5 (although probably only
> trace4 was apparently really necessary), and I've had trouble generally
> identifying the entries related to this specific rule, but here are a few
> entries I think represent what's happening.
>
>
> https://webstage.example.com/administrator/sqlantern-joomla/php/?cms_settings
> [Tue Jan 16 08:55:13.289102 2024] [rewrite:trace3] [pid 904886:tid 904938]
> mod_rewrite.c(493): [client 68.111.193.42:0] 68.195.193.42 - - [
> webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial
> <http://webstage.example.com/sid#560ba7f01a68][rid%237f2b1c0bc210/initial>]
> applying pattern '^(.*)/$' to uri '/administrator/sqlantern-joomla/php/'
>
> [Tue Jan 16 08:55:13.289129 2024] [rewrite:trace4] [pid 904886:tid 904938]
> mod_rewrite.c(493): [client 68.195.193.42:0] 68.111.193.42 - - [
> webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial
> <http://webstage.example.com/sid#560ba7f01a68][rid%237f2b1c0bc210/initial>]
> RewriteCond: input='/administrator/sqlantern-joomla/php/' pattern='!-d' =>
> matched
>
> [Tue Jan 16 08:55:13.289143 2024] [rewrite:trace2] [pid 904886:tid 904938]
> mod_rewrite.c(493): [client 68.111.193.42:0] 68.195.193.42 - - [
> webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial
> <http://webstage.example.com/sid#560ba7f01a68][rid%237f2b1c0bc210/initial>]
> rewrite '/administrator/sqlantern-joomla/php/' ->
> '/administrator/sqlantern-joomla/php'
>
> [Tue Jan 16 08:55:13.289156 2024] [rewrite:trace2] [pid 904886:tid 904938]
> mod_rewrite.c(493): [client 68.111.193.42:0] 68.195.193.42 - - [
> webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial
> <http://webstage.example.com/sid#560ba7f01a68][rid%237f2b1c0bc210/initial>]
> explicitly forcing redirect with
> https://webstage.example.com/administrator/sqlantern-joomla/php
>
> [Tue Jan 16 08:55:13.289181 2024] [rewrite:trace1] [pid 904886:tid 904938]
> mod_rewrite.c(493): [client 68.111.193.42:0] 68.195.193.42 - - [
> webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial
> <http://webstage.example.com/sid#560ba7f01a68][rid%237f2b1c0bc210/initial>]
> escaping https://webstage.example.com/administrator/sqlantern-joomla/php
> for redirect
>
> [Tue Jan 16 08:55:13.289195 2024] [rewrite:trace1] [pid 904886:tid 904938]
> mod_rewrite.c(493): [client 68.111.193.42:0] 68.195.193.42 - - [
> webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial
> <http://webstage.example.com/sid#560ba7f01a68][rid%237f2b1c0bc210/initial>]
> copying cms_settings to query string for redirect
>
> [Tue Jan 16 08:55:13.289205 2024] [rewrite:trace1] [pid 904886:tid 904938]
> mod_rewrite.c(493): [client 68.111.193.42:0] 68.195.193.42 - - [
> webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial
> <http://webstage.example.com/sid#560ba7f01a68][rid%237f2b1c0bc210/initial>]
> redirect to
> https://webstage.example.com/administrator/sqlantern-joomla/php?cms_settings
> [REDIRECT/301]
>
> Thanks,
> Dave
>
>
>
Based on those log entries, the rewrite worked fine.

Perhaps you're dealing with DirectorySlash here.

Re: [users@httpd] Removing trailing slashes with query strings

Posted by Dave Wreski <dw...@guardiandigital.com.INVALID>.
Hi,

>     I have the following rule that works well to remove trailing
>     slashes from URLs:
>
>     RewriteCond %{REQUEST_FILENAME} !-d
>     RewriteRule ^(.*)/$ $1 [R=301,L]
>
>     This is done to prevent the non-slash URL from being treated as
>     duplicate content with the URL with a slash.
>
>     The problem is that there are exceptions, such as when we want to
>     add query strings to the end of a URL:
>
>     https://example.com/administrator/path/?cms_settings
>
>     I've also tried a variation, like:
>
>     https://example.com/administrator/path?cms_settings
>
>     but somehow the slash is replaced back into the URL and I don't
>     know where it comes from.
>
>     Adding index.php to the path works properly because it's then not
>     a directory:
>
>     https://example.com/administrator/path/index.php?cms_settings
>
>     Any ideas greatly appreciated.
>
>     Thanks,
>     Dave
>
>
>
> What does the rewrite log say, exactly?

There's a tremendous amount of noise at trace5 (although probably only 
trace4 was apparently really necessary), and I've had trouble generally 
identifying the entries related to this specific rule, but here are a 
few entries I think represent what's happening.

https://webstage.example.com/administrator/sqlantern-joomla/php/?cms_settings

[Tue Jan 16 08:55:13.289102 2024] [rewrite:trace3] [pid 904886:tid 
904938] mod_rewrite.c(493): [client 68.111.193.42:0] 68.195.193.42 - - 
[webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial] 
applying pattern '^(.*)/$' to uri '/administrator/sqlantern-joomla/php/'

[Tue Jan 16 08:55:13.289129 2024] [rewrite:trace4] [pid 904886:tid 
904938] mod_rewrite.c(493): [client 68.195.193.42:0] 68.111.193.42 - - 
[webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial] 
RewriteCond: input='/administrator/sqlantern-joomla/php/' pattern='!-d' 
=> matched

[Tue Jan 16 08:55:13.289143 2024] [rewrite:trace2] [pid 904886:tid 
904938] mod_rewrite.c(493): [client 68.111.193.42:0] 68.195.193.42 - - 
[webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial] 
rewrite '/administrator/sqlantern-joomla/php/' -> 
'/administrator/sqlantern-joomla/php'

[Tue Jan 16 08:55:13.289156 2024] [rewrite:trace2] [pid 904886:tid 
904938] mod_rewrite.c(493): [client 68.111.193.42:0] 68.195.193.42 - - 
[webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial] 
explicitly forcing redirect with 
https://webstage.example.com/administrator/sqlantern-joomla/php

[Tue Jan 16 08:55:13.289181 2024] [rewrite:trace1] [pid 904886:tid 
904938] mod_rewrite.c(493): [client 68.111.193.42:0] 68.195.193.42 - - 
[webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial] 
escaping https://webstage.example.com/administrator/sqlantern-joomla/php 
for redirect

[Tue Jan 16 08:55:13.289195 2024] [rewrite:trace1] [pid 904886:tid 
904938] mod_rewrite.c(493): [client 68.111.193.42:0] 68.195.193.42 - - 
[webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial] 
copying cms_settings to query string for redirect

[Tue Jan 16 08:55:13.289205 2024] [rewrite:trace1] [pid 904886:tid 
904938] mod_rewrite.c(493): [client 68.111.193.42:0] 68.195.193.42 - - 
[webstage.example.com/sid#560ba7f01a68][rid#7f2b1c0bc210/initial] 
redirect to 
https://webstage.example.com/administrator/sqlantern-joomla/php?cms_settings 
[REDIRECT/301]

Thanks,
Dave


Re: [users@httpd] Removing trailing slashes with query strings

Posted by Frank Gingras <th...@apache.org>.
On Mon, Jan 15, 2024 at 10:16 PM Dave Wreski
<dw...@guardiandigital.com.invalid> wrote:

> Hi,
>
> I have the following rule that works well to remove trailing slashes from
> URLs:
>
> RewriteCond %{REQUEST_FILENAME} !-d
> RewriteRule ^(.*)/$ $1 [R=301,L]
>
> This is done to prevent the non-slash URL from being treated as duplicate
> content with the URL with a slash.
>
> The problem is that there are exceptions, such as when we want to add
> query strings to the end of a URL:
>
> https://example.com/administrator/path/?cms_settings
>
> I've also tried a variation, like:
>
> https://example.com/administrator/path?cms_settings
>
> but somehow the slash is replaced back into the URL and I don't know where
> it comes from.
>
> Adding index.php to the path works properly because it's then not a
> directory:
>
> https://example.com/administrator/path/index.php?cms_settings
>
> Any ideas greatly appreciated.
>
> Thanks,
> Dave
>
>
>
What does the rewrite log say, exactly?