.htaccess rewrite all characters

20,982

Solution 1

(.*) will match all characters. That's all you need.

Solution 2

To match everything you can just use: ^(.+)/(.+)/?$ (with optional trailing slash)

If you want to stick with the existing format using character classes, you have a couple of problems here. Inside character sets/classes, you should always put hyphens at the end for a valid regex. You've also omitted uppercase characters. So to match a basic alphanumeric, underscore and hyphen you want:

[a-zA-Z0-9_-]

or, using character escapes:

[\w\d_-]

Your second bracket set is currently specifically excluding period and slash, matching all others.

If your application data is well-defined you may want to go with defined character classes, and 404 on all other data. If you want the blanket redirect, then the following should fix you up:

RewriteRule ^(.+)/(.+)/?$ index.php?engine=$1&q=$2 [NC,L]
Share:
20,982
armand
Author by

armand

Updated on July 05, 2022

Comments

  • armand
    armand almost 2 years

    What pattern do I need to rewrite all characters in the requested URL? I tried:

    RewriteRule ^([a-z\-_0-9]+)/([^/.]+)$ index.php?engine=$1&q=$2 [NC,L]
    

    But the dots . are not getting recognized.

  • armand
    armand over 13 years
    but im getting some troubles with the images on the site, they are not appearing if I put the ruke like: ^([a-z\-_0-9]+)/(.*)$ index.php?engine=$1&q=$2any clue?
  • armand
    armand over 13 years
    it is working (both answers (*) and yours @tomwalsham but why are the images disappearing? as the path where not ok or re-chaged, any clues?
  • tomwalsham
    tomwalsham over 13 years
    You will want to use the <base href='example.org'> tag to reset all relative references on the site to base, as with mod_rewrite they are interpreted as being executed in subfolders.
  • armand
    armand over 13 years
    mmm is not working, are u sure it is a problem of the php file and not of the .htaccess?
  • tomwalsham
    tomwalsham over 13 years
    If the page is loading fine, but the images are broken, this is either a problem of the relative paths of the images being broken by the mod_rewrite, or that you're also rewriting the image paths to go to the index.php In Firefox you can RightClick->View Image to see if you're loading your main file. Alternatively, you will need to use a less broad regex, or a rewritecond.
  • tomwalsham
    tomwalsham over 13 years
    RewriteCond %{REQUEST_URI} !^/images/ before the RewriteRule line may help you (assuming your images are all contained within /images/
  • armand
    armand over 13 years
    aww nothing! how could I add the dots on the last section ([^/.]+) for my rule ^([a-z\-_0-9]+)/([^/.]+)$ index.php?engine=$1&q=$2 if I take out the /. I have the same problem with the src's