.htaccessmod-rewriteurl-rewritingurl-masking

Mod_Rewrite for constantly changing url


I have a url, lets say its blog.art.ca/Customer/AAAABBBB/index.html and I want to hide Customer and AAAABBBB. Now AAAABBBB can be any 8 character alphanumeric code.

Options +FollowSymLinks -Multiviews -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^Customer/(.*)$ /$1 [L,R]
</IfModule>

I've tried numerous things, however, I either get ERR_TOO_MANY_REDIRECTS or it just causes the server to crash. Any suggestions would be much appreciated!


Solution

  • You still need to allow the requests to reach the actual requested resources, so you'll need two sets of rules, one to the browser, to show the new shorter URL (301 permanent redirect), and a second set to undo this mapping back to the original URL, so that Apache can find the right stuff to serve eg.

    RewriteBase /
    
    # Remove Customer/AAAABBB from the URL shown in the browser.
    RewriteCond %{QUERY_STRING} !customer=
    RewriteRule ^Customer/([^/]+)/(.*) /$2?customer=$1 [L,QSA,R=301]
    
    # Internally undo any masked rewrites.
    RewriteCond %{QUERY_STRING} customer=([^&]+)
    RewriteRule (.*)  /Customer/%1/$1 [L]