RewriteRule that accommodates numbers and words

The first rule below is one that I have been using for years. Now, in addition to numbers in the URL, I would like to accept a username, like so:

example.com/12345
example.com/fatcat1970

However, when I test out my htaccess using the following code, the second one doesn’t work. Is there a way to do an IF statement so that it properly detects if a number or word is in the URL?

RewriteRule ^/?([0-9]+)/?$ index.php?x=$1 [L]
RewriteRule ^/([a-zA-Z0-9\+-]+)$ index.php?sponsorUsername=$1 [L]

Thanks!

This is what I have been using for years. The RewriteRule redirects everything to index.php:

  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f          
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L] 


I think my final rule should be replaced with the following:

  RewriteRule ^(.*)$ index.php?sponsorUsername=$1 [L]

# or even more strict and prevents any directories

  RewriteRule ^(.+)$ index.php?sponsorUsername=$1 [L]



Thanks for your speedy response John! Could you take it a step further and elaborate what your code is doing? I’m still a newbie when it comes to ModRewrite.

And how does your code accommodate a number or a name when, in the end, it appends the value to the sponsorUsername? In the code I have been using, the index.php?x=$1 refers to a number.

Thanks

Not only does it redirect everything, it also redirects anything and nothing.

roughly translated
^(.*)$
begin - capture zero or more of any character(s) - end

To quote dklynn

1 Like

My knowledge is limited on the subject but I will try and look forward to being corrected by some knowledgeable members.

### command to implement RewriteCondition AND RewriteRule
    RewriteEngine on

### reads command line and interprets as a file
    RewriteCond %{REQUEST_FILENAME} !-f  
        
### reads command line and interprets as a directory
    RewriteCond %{REQUEST_FILENAME} !-d

### interpret, execute and route RewriteCond to specific root file including directories
    RewriteRule ^(.*)$ index.php/$1 [L]

### interpret, execute and route RewriteCond to specific root file excluding directories
    RewriteRule ^(.+)$ index.php?sponsorUsername=$1 [L]

The code specified allows everything and nothing to be passed to the index.php sponsorUsername parameter.

I find it far easier to use PHP to test or eliminate prohibited characters.

If the desired request exists the response is routed and activated, If it does not exist then an attempt is made to search and route to a nearest match otherwise fall through to a final 404,php file.

Mitt is quite correct about the DANGER of using the :fire: EVERYTHING :fire: (or NOTHING) atom but J_B correctly provided an exit to the loop ^(.*)$ normally causes.

That said, it seem as if the OP is asking to upgrade [0-9]+ to [a-z]*[0-9]+ (translation: one or more digits to ZERO or more lowercase letters followed by at least one digit). As usual, better specification of the acceptable x or SponsorUserName would get a better answer.

Finally, if the OP really wants to learn about coding mod_rewrite (with plenty of examples), http://dk.co.nz/seo. It’s helped MANY members over the years and should help here, too.

Regards,

DK

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.