Forum Moderators: phranque

Message Too Old, No Replies

What am I missing?

Rewrite with numbers

         

rocknbil

2:19 am on Aug 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Scenario: anytihng in a directory redirects to a script.

When I do this

RewriteRule ^abc-page08-1.*$ /abc-page08-11/some-script.php [L]

but this

RewriteRule ^abc-page08-11.*$ /abc-page08-11/some-script.php [L]

Gets ignored and uses the 404. I thought

RewriteRule ^abc-page08-1\1.*$ /abc-page08-11/some-script.php [L]

But no, failed also. Grains of advice?

lucy24

6:31 am on Aug 10, 2011 (gmt 0)

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



When I do this

RewriteRule ^abc-page08-1.*$ /abc-page08-11/some-script.php [L]

I'm looking for a verb right around here. What happens when you do this? Other than the predictable, which is that g1 will shortly come along and read the riot act about .* (In this case, the whole .*$ is unnecessary since you're not capturing it.) Also about the leading / in your target, because apparently not all servers let you do this. (Mine doesn't mind. That lowly / changes a rewrite into a redirect without having to type the whole http: thing at the beginning.)

but this

RewriteRule ^abc-page08-11.*$ /abc-page08-11/some-script.php [L]

Gets ignored and uses the 404.

The 404 refers to what emerges from the far end of htaccess. If the rule isn't applied, it means that the requested page wasn't called /abc-page08-11{maybemorestuff}. This leaves a lot of room for things that the page really was called, including a whole lot of nonexistent pages.

I thought

RewriteRule ^abc-page08-1\1.*$ /abc-page08-11/some-script.php [L]

Your browser has an appetite for verbs, doesn't it? What did you think this version would do? In htaccess you don't escape numerals, so it probably thinks you're looking for something with a literal backslash.

How are you testing this? By clicking actual links on your site, or by typing in random addresses? What's the exact name of the directory you're trying to grab?

g1smd

6:38 am on Aug 10, 2011 (gmt 0)

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



anything in a directory redirects to a script.
This is not a redirect. It is an internal rewrite. It would be a redirect if the target contained http+domain name and/or the [R] flag. You do not want a redirect here. Rewrite is correct.

^abc-page08-1.*$
The 1.*$ means "after the one, allow any number of digits, characters, and/or punctuation, or nothing in the path". Is that what you actually want?

If it is, then the pattern simplifies to
^abc-page08-1
but this allows infinite duplicate content; as any URL variation will return content with "200 OK" status. You should allow only one canonical URL to be rewritten and serve content.

If the .* is intending to allow attached parameters to pass through, be aware that RewriteRule sees only the path part of the URL. Parameters are automatically re-appended unless you explicitly specify different parameters or clear the parameters (with a single question mark).

You didn't mention the test URL, so all of the above is a guess. Use example.com to stop forum auto-linking.

g1smd

6:51 am on Aug 10, 2011 (gmt 0)

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



That lowly / changes a rewrite into a redirect without having to type the whole http: thing at the beginning.
It should not do that. Only http+domain and/or [R] should result in a redirect. Whenever I see that behaviour it has been because there is a redirect listed after that rewrite or there is a redirect affecting the current request defined in a .htaccess file in a subfolder. Both of those expose the rewritten filepath back out on to the web as a new URL.

lucy24

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

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



:: detour for quick experiment in a private directory ::

Whew. You're right. I'd been thinking of the R=301 as belt-and-suspenders, but it's essential.

Leaving out the R also dramatically illustrates the difference between Rewrite and Redirect when you use relative links for your css :)

g1smd

7:30 am on Aug 10, 2011 (gmt 0)

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



Yes, to summarise:
- target contains http+domain and no [R] flag - 302 redirect
- target contains [R] flag (with or without http+domain) - 302 redirect
- target contains [R=302] flag (with or without http+domain) - 302 redirect
- target contains [R=301] flag (with or without http+domain) - 301 redirect
- target contains path only and no [R] flag - internal rewrite

In general, you're more likely to end up with a 302 redirect than anything else if you're not sure what you're doing and start "guessing" at code.

Redirects should include http+domain in the target otherwise you end up with an unwanted multiple step redirection chain when a request has two problems that need to be fixed by redirecting.

Additionally, every RewriteRule should always have the [L] flag every time (unless you know exactly why it should be omitted).

lucy24

9:25 am on Aug 10, 2011 (gmt 0)

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



Additionally, every RewriteRule should always have the [L] flag every time (unless you know exactly why it should be omitted).

Well, I'm not touching [C] or [S] with a barge pole. But somewhere hereabouts I recently translated [F,L] as "Drop dead and then stop what you're doing."

rocknbil

4:35 pm on Aug 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is not a redirect. It is an internal rewrite.


I know, and know it's one of your peeves, sorry . . . rewrite is what I'm after, hence no R...

The 1.*$ means "after the one, allow any number of digits, characters, and/or punctuation, or nothing in the path". Is that what you actually want?

You didn't mention the test URL,


Yes, which should become more clear seeing the test uri's, and I thought of that (test url) on the bike ride home. Long day. This should be a directory-restricted set of rewrites, anything in that directory dire- err, writes - to the script.

These are landing pages, they will not be indexed, duplicate content is not an issue (but lesson learned for ones that do . . . )

example.com/abc-page08-11/
example.com/abc-page08-11 (these, having no terminal "uri/path," would output an "index" of all the "pages")

example.com/abc-page08-11/landing-page-one
example.com/abc-page08-11/landing-page-two
example.com/abc-page08-11/landing-page-three

... so zero or more of any character is probably ok, though I could probably work with [\d\w\-\_]*, the uri's are client-controlled and could potentially be anything. I hadn't gotten as far as a pass through, though that would be useful. The real question is why, when testing these URL's, this works (for all of them)

RewriteRule ^abc-page08-1.*$ /abc-page08-11/some-script.php [L]

But this is ignored and cascades to the 404 rule.

RewriteRule ^abc-page08-11.*$ /abc-page08-11/some-script.php [L]

A supplemental, this .htaccess was initially intended to be housed in the directory abc-page08-11, but have experimented with it at root as well, nearly identical results.

rocknbil

5:43 pm on Aug 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<sigh> A morning ride to work through the early S.D. fog always clarifies things . . .

Per directory rule that works fine (located inside abc-page08-11):

RewriteEngine On
# EDIT: oops, NO --> RewriteRule ^.*$ some-script.php [L]
RewriteRule ^([^.]+)?$ some-script.php [L]

No need to go out to root.

Still curious about the number thing though. One 1, it worked, two, fail.

g1smd

6:20 pm on Aug 10, 2011 (gmt 0)

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



Yes,
[F]
is one of those rare occasions when you should omit the 'L' because you know exactly why it should be omitted.


I assume an infinite rewrite loop is the failure mode for the one "not working"? You need a preceding RewriteCond to exclude requests for the script name from being rewritten again.