Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite: append end slash to url

problem correcting urls

         

pixeline

3:12 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



Hello again!

i'm having a mod_rewrite issue: i would like that

/projects/touch redirects to /projects/touch/

but so far i get erratic results: here is the htaccess file:


RewriteRule ^([A-Za-z0-9-]+)$ $1/ [R]
RewriteRule ^([A-Za-z0-9-]+)/$ myapp.php?section=$1 [L]

what happens is, if i try this:

http://example.com/projects

i get to this:

http://example.com/var/www/my-domain/domains/m2.my-domain.com/public_html/projects/

so it appends the DOCUMENT_ROOT to the url ...

strange no?

Thanks for any insight!

Alex

[edited by: jdMorgan at 3:50 pm (utc) on Jan. 23, 2008]
[edit reason] example.com [/edit]

jdMorgan

3:19 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use [R=301,L] on the first rule to avoid this.

Jim

pixeline

4:17 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



can you explain?

it works for my example, but i have other rewrite and the same issue arises, but adding the L flag does not solve it this time, same happens, the document_root gets inserted in the path

here is my updated htaccess:


Options +FollowSymlinks
RewriteEngine on
# FOUR PARAMS
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ $1/$2/$3/$4/ [R=301,L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ app.php?section=$1&sortBy=$2&dsAnchor=$3&item=$4
#
# TWO PARAMS
RewriteRule ^projects/([A-Za-z0-9-]+)$ projects/$1/ [R=301,L]
RewriteRule ^projects/([A-Za-z0-9-]+)/$ app.php?section=projects&sortBy=time&dsAnchor=%{TIME_YEAR}&item=$1
#
# ONE PARAM
RewriteRule ^/([A-Za-z0-9-]+)$ /$1/ [R=301,L]
RewriteRule ^([A-Za-z0-9-]+)/$ metalab2.php?section=$1 [L]
#--------------------------------------------

jdMorgan

4:26 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use [L] on every rule (unless you have a very good reason not to) to avoid this documented Apache mod_rewrite bug [archive.apache.org].

Note also that although the 'disposition' of this bug says that it will be fixed in Apache 2.x, my testing indicates that it still exists in Apache2.

Also, I suggest that you always use a canonical form for the substitution URL when coding an external redirect, as in:


RewriteRule ^([a-z0-9-]+)$ http://www.example.com/$1/ [NC,R=301,L]

Note the use of [NC] to make the pattern compare case-insensitive, and so shorten the pattern.

Jim

jdMorgan

4:35 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could also replace *all* of the add-a-slash rules with a single one:

# If URL-path does not contain a period or end with a slash
RewriteCond %{REQUEST_URI} !(\.¦/$)
# Externally redirect to add a trailing slash
RewriteRule (.*) http://www.example.com/$1/ [R=301,L]

or, using your more-restrictive explicit pattern from the code above:

# Externally redirect to add missing trailing slash
RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ http://www.example.com/$1/ [NC,R=301,L]

In both cases, it does not matter how many directory levels are present in the requested URL-path; Either of these rules handles all of them. Pick the one that best suits your site's URL design.

Replace the broken pipe "¦" character with a solid pipe character before use; Posting on this forum modifies the pipe characters.

Jim

pixeline

4:57 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



Jim, these are all super good tips, thanks a lot!

Indeed, now it works but i'm stumbling on another caveat: i 'm using some javascript that was analysing the urls for the pair variable=value and triggering different animations out of it.

because of url_rewrite,this does not work anymore, unfortunately. Right now i'm thinking i could maybe recompose the ugly urls via javascript before parsing, yet it's problematic because i don't know in advance how many variables will be used.

Do you know of any clever way to deal with that? maybe have php write the dynamic ugl url inside the generated html and retrieve that value in javascript?

pixeline

5:20 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



is it also the forum that adds the escape slashes before the "-" or is it intentional in:


# Externally redirect to add missing trailing slash
RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ http://www.example.com/$1/ [NC,R=301,L]

also, is there a way to obtain in one line all the query variables, each in its $index?

Apologies if i'm asking stupid questions: i have to catch up on regex at the same time, and your advises are super helpful.