apache.htaccessurl-masking

.htaccess URL masking instead of redirecting


I'm trying to mask URL in away like while the real URL is www.somedomain.com/subfolder/index.php?p=page3 it shows www.somedomain.com/subfolder/page3 , I know such questions have been asked a lot on here and I did searched a lot but most of results didn't cater what I am looking for, also I almost know nothing but the basics about htaccess tweaking.

one result I got when i searched has the following htaccess code:

Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^index.php$ %{QUERY_STRING} [C]
RewriteRule p=(.*) www.somedomain.com/subfolder/$1? [R=301,L]

It did the trick concerning the URL re-writing but it redirects the page instead of just masking the URL and it gives a 404 error, even when I removed the R=301 it didn't work.

Also the code above gives a 500 server error when I launch the index page of the subfolder www.somedomain.com/subfolder/.

Finally I'd like to mention that I'm testing with a sub-directory in localhost in case it make difference because some codes I've found on the web either just don't work at all or they give 500 server error.

Many Thanks in Advance.


Solution

  • You may try this:

    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /
    # Prevent loops
    RewriteCond %{REQUEST_URI} !index\.php         [NC]
    RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+)/? [NC]
    RewriteRule .*  %1/index.php?page=%2           [L]
    

    Maps silently:

    http://www.somedomain.com/anyfolder/val with or without trailing slash.

    The above is the URL entered and shown in the browser's address bar.

    To:

    http://www.somedomain.com/anyfolder/index.php?p=val

    Strings anyfolder and val are assumed to be dynamic.

    For permanent redirection, replace [L] with [R=301,L].