2

I need htaccess rule that rewrites an uri into get variable if it is not for an existing file or folder. Example:

www.example.com/page1.php -> goto page1.php
www.example.com/page2.html -> goto page2.html
www.example.com/folder -> goto /folder
www.example.com/some_string_value -> rewrite as /default.php?value=some_string_value
  • 1
    You might have to handle this in the code and redirect as appropriate. Htaccess won't know if this is a 404 or 200. It just routes it as you want it to. – scgough Jun 05 '15 at 06:30
  • 1
    I apologize if I come across as rude or elitist, but this is not a request forum. You need to show the community you have made an attempt to research and that you are posting because you were unable to understand/comprehend what is going on. Simply saying "I need x" will not work around here. Sorry. – Tarquin Jun 05 '15 at 06:38
  • possible duplicate of [How to write htaccess rewrite rule for seo friendly url](http://stackoverflow.com/questions/28168375/how-to-write-htaccess-rewrite-rule-for-seo-friendly-url) – Tarquin Jun 05 '15 at 06:43

1 Answers1

6

That's really a very common request. There are probably 1000's of questions about this asked on this site. You need to use the RewriteCond and REQUEST_FILENAME to look for non existent folder and then internally rewrite to get variable. Essentially if it's a 404 (non existent URI) it will be routed to your default.php file. That's how pretty URL's are done. You can put this in your .htaccess file in the root.

RewriteEngine On
#prevent the use of the default.php file directly and redirect to friendly URI
RewriteCond %{THE_REQUEST} [A-Z]{3,9}\ /default\.php\?value=([^&\ ]+)
RewriteRule ^ /%1? [R=301,L]
#redirect non existent (404) folder to get variable
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /default.php?value=$1 [L]
Panama Jack
  • 24,158
  • 10
  • 63
  • 95
  • 1
    I find it frustrating that well thought questions are ignored, yet requests for code are fulfilled on this site. It should be flagged as a duplicate and moved on, however the quality of your answer still deserves an upvote. I just wanted to make it clear the frustration of the patterns of answering occuring here lately. – Tarquin Jun 05 '15 at 07:08
  • 2
    @Tarquin thanks and you do have a point and that's why I at least try to provide a little insight into what he needed instead of just giving the code. There should have been some effort shown. I've been there once so just trying to help others. :) – Panama Jack Jun 05 '15 at 07:14
  • 3
    I fully understand. I too want to give back to SO as it has been kind to me for years too. – Tarquin Jun 05 '15 at 07:15
  • @PanamaJack thank you for the solution, it was indeed very helpful. – Nitish Raj Uprety Jun 05 '15 at 07:26
  • 1
    @Tarquin I am sorry if my way of questioning bothered you. I am fairly new at putting questions in this site but one thing is for sure that this site has helped me a lot in past and I assure you that I would keep your suggestions into consideration while posting questions in near future. – Nitish Raj Uprety Jun 05 '15 at 07:26