2

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.

Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
  • 1
    I marked my answer for deletion - I confused myself by listing your destination first and your source second, as you had listed them in your question, so I had them reversed in the response (it's generally easier to write out what you're doing in the same order things appear in the rule - source, then destination). @faa's answer gets the order right. Sorry about that. – David Ravetti Feb 18 '13 at 04:48

1 Answers1

3

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].

Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37
  • This answer is correct, just be aware (@JEES) that the second RewriteCond is specific to a two-level directory structure, as in the example in your question. If you need to apply it to a different number of directory levels you'll need to adjust it. – David Ravetti Feb 18 '13 at 04:51
  • I think I'm confusing everybody, I apologize for that but what I'm trying to do is to convert my so called "ugly URL" which is produced by dynamic page insertion `www.somedomain.com/subfolder/index.php?p=page3` into a more appealing and SEO friendly "nice URL" like this one `www.somedomain.com/subfolder/page3` Or even into `www.somedomain.com/subfolder/pages/page3` while still loading the same page and injecting same query string. – Mi-Creativity Feb 18 '13 at 13:17
  • `www.somedomain.com/subfolder/page3` that's what the rule in my answer does. Try it. Needless to say `www.somedomain.com/subfolder/page3` is the URL linked to or entered and shown in the browser's address bar. If it works for you, I can modify the answer for any number of levels in the directory structure, but, again, give it a try first. – Felipe Alameda A Feb 18 '13 at 13:26
  • @faa yes that worked it just needed some adjustment concerning the directory level everything is like charm now after I added another sub directory **([^/]+)** with (its corresponding **%3** .Thanks a lot @faa and @David for your help and time, just for the last part of the question I appended to the above code the following line to get rid of index.php `RewriteCond %{THE_REQUEST} /index.php RewriteRule ^index.php$ http://www.somedomain.com [R=301,L]` Many thanks again. – Mi-Creativity Feb 18 '13 at 20:35
  • I got a problem similar to this, please see if you could help me. Here's the link http://stackoverflow.com/q/37046157/2549581 – vs_lala May 05 '16 at 09:38