Welcome to WebmasterWorld Guest from 54.145.117.60

Forum Moderators: Ocean10000 & incrediBILL & phranque

mod rewrite that only matches numbers

Numbers re-directing to query string...

     
1:06 pm on Sep 30, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member trillianjedi is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 15, 2003
posts: 7254
votes: 0


Hi all,

I'm stuck in the mod_rewrite voodoo again :)

I had what I thought was a really simple task - I want to match a top level directory, only if it consists purely of numbers, and convert that to a query string, so:

https://example.com/1234

... is internally mapped (eg no redirect) to:

https://example.com/index.html?number=1234

I have this so far:


RewriteEngine on
RewriteRule ^([0-9]+)$ index.html?number=$1


But this is just giving me a 404 and isn't hitting my index.html page. I have validated that mod_rewrite is installed, and I did a service apache2 reload and a restart after adding this to my /etc/apache2/sites-available/default-ssl.conf config file.

A had a perusal through stack overflow and lots of people seem to be using this syntax without any issue, so I'm not sure what I've done wrong - any ideas?

Thanks!
1:23 pm on Sept 30, 2017 (gmt 0)

Senior Member from GB 

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

joined:Nov 16, 2005
posts:2703
votes: 97


I think the path it matches against includes the URL. Have you tried:

RewriteRule ^/([0-9]+)$ index.html?number=$1
1:39 pm on Sept 30, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member trillianjedi is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 15, 2003
posts: 7254
votes: 0


Hi Graeme, thanks.

I hadn't tried that, so I just did and get:

https://example.com/1234

400 Bad Request

and with the trailing slash, eg:

https://example.com/1234/

... I get a 404.

In the error log I see this:

No matching DirectoryIndex (index.html,index.cgi,index.pl,index.php,index.xhtml,index.htm) found, and server-generated directory index forbidden by Options directive


... for the one with the trailing slash. Which kind of makes sense (no index.html etc in that folder) but I'm not sure if that means my RegEx is failing to match?

I seem to recall there's a way to enable mod_rewrite logging so I'll go google how to do that and see if I can get any further insights.
1:47 pm on Sept 30, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member trillianjedi is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 15, 2003
posts: 7254
votes: 0


OK, got the mod_rewrite logging working, and this is what I see in /var/log/apache2/error.log when I try without the trailing slash:

https://example.com/1234


init rewrite engine with requested uri /1234
applying pattern '^/([0-9]+)$' to uri '/1234'
rewrite '/1234' -> 'index.html?number=1234'
split uri=index.html?number=1234 -> uri=index.html, args=number=1234
local path result: index.html


I can't see anything in there that looks obviously wrong?

In the browser I get a 400 Bad Request
2:15 pm on Sept 30, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member trillianjedi is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 15, 2003
posts: 7254
votes: 0


Got it, I needed the forward slash on the rule as well as the match:

RewriteRule ^/([0-9]+)$ /index.html?number=$1


Thanks for the help!
4:53 pm on Sept 30, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14258
votes: 551


I needed the forward slash on the rule as well as the match:
Now, wait. That can't possibly be right. mod_rewrite in a directory context (whether htaccess or a <Directory> section of config) does not match against the opening slash. The only exception is if you’ve got this rule lying loose in your config file, which would be exceedingly unusual.

You do absolutely need a leading / in the target so the rule knows what it's rewriting to.

What's more serious is that every time in this thread I've seen the rule cited, it is missing its [L] flag. Was the flag intentionally left off to permit other rules to execute as well? What other, subsequent RewriteRules are there? At a minimum, does this rule come after all external redirects?
7:52 pm on Sept 30, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member trillianjedi is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 15, 2003
posts: 7254
votes: 0


Hi Lucy,

Thanks for your input and for raising those points. You were right, and I guess this highlights my lack of knowledge/experience in this stuff.

I have moved the relevant bits into the <directory> section of the config and indeed that did mean that I could remove the first leading slash, leaving it only in the to section.

What other, subsequent RewriteRules are there? At a minimum, does this rule come after all external redirects?


There are no other rules, or external redirects. I've added the [L] to make that a verbose instruction though - good catch, thanks :)
10:21 pm on Oct 1, 2017 (gmt 0)

Senior Member from GB 

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

joined:Nov 16, 2005
posts:2703
votes: 97


Lucy, I know you are right about not needing the leading slash, but the reason I got confused was that this uses them:

[httpd.apache.org...]

any idea why?
12:58 am on Oct 2, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14258
votes: 551


Jiminy, that's an old link. 2.0! A lot of the time you can hop straight over to the current version of a given document, but this time it doesn't seem to exist.

URLs in mod_rewrite patterns have a leading / slash if the RewriteRules are lying loose in the config file (not in a <Directory> section and not in htaccess). This is of course perfectly legal--but when the heck would you ever do it in real life? At a minimum you'd have one directory for the actual site, and another one for mail, and another for php ...

Buried in the middle of the article on htaccess in 2.4 [httpd.apache.org] (if the fragment gets eaten, use the link to “Rewrite Rules in .htaccess files”) is a capsule summary of how the / slash works. Note that even there it's a little misleading because it simply says "in httpd.conf" when it should say “in httpd.conf but not in a <Directory> section”.
5:00 pm on Oct 9, 2017 (gmt 0)

Senior Member from GB 

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

joined:Nov 16, 2005
posts:2703
votes: 97


Thanks Lucy, I could not find the 2.4 version of that.

On most sites I do, most of my rewrite rules are in the config file, or in a <VirtualHost>, mostly redirects to canonicalise between www. and bare domain and similar.
10:08 pm on Oct 11, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14258
votes: 551


Ah, yes, that's the tricky bit. A <VirtualHost> looks exactly the same as a <Directory> section, but it isn't. So the leading / slash really is legitimate there. I will try to remember this.
12:27 am on Oct 12, 2017 (gmt 0)

Administrator

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

joined:Aug 10, 2004
posts:11080
votes: 106


I know you are right about not needing the leading slash, but the reason I got confused was that this uses them:

[httpd.apache.org...]

any idea why?


https://httpd.apache.org/docs/2.0/misc/rewriteguide.html#ToC2
ATTENTION: Depending on your server-configuration it can be necessary to slightly change the examples for your situation, e.g. [...] rewriting a ruleset to fit in .htaccess context instead of per-server context. Always try to understand what a particular ruleset really does before you use it. It avoid problems.


I could not find the 2.4 version of that.

this would be the current equivalent of the old "URL Rewriting Guide".
Apache mod_rewrite - Apache HTTP Server Version 2.4:
http://httpd.apache.org/docs/current/rewrite/ [httpd.apache.org]
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members