[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

RewriteRule in htaccess problem

Posted on 2004-11-11
10
Medium Priority
?
992 Views
Last Modified: 2006-11-17
Hi everyone, I really hope someone can help me?

I'm trying to get my urls to look like this:

http://www.mydomain.com/products/subproduct/detail

...when the actual file is an html file to be 'include'd in a index.php for example:

http://www.mydomain.com/products/index.php?p=subproduct/detail

...my index.php file would look something like this:
<html>
... blah blah ...
<?php
  clearstatcache();
  if (file_exists($HTTP_GET_VARS[p].".htm")) {
    $page = $HTTP_GET_VARS[p];
    echo "The file filename exists";
  } else {
    $page = 'products';
    echo "The file filename does not exist";
  }
  include($page.".htm");
?>
... blah blah ...
</html>

...i've tried this in my htaccess file:

RewriteEngine On
Options +FollowSymlinks
RewriteRule ^products/(.*) /products/index.php?p=$1

Please can someone help me as this is becoming a huge headache!
This is probably fairly easy to someone who knows apache rewrite stuff.

Thank you,

Rob Holmes
0
Comment
Question by:robholmes83
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 10

Expert Comment

by:eeBlueShadow
ID: 12554372
your problem is probably that, when you redirect someone to /products/index.php?p=whatever, that redirect is picked up by the RewriteRule *again* and converted to /products/index.php?p=index.php?p=whatever etc etc.

Add the 'Last' flag to the rule and it should work:

RewriteRule ^products/(.*) /products/index.php?p=$1 [L]

_Blue
0
 

Author Comment

by:robholmes83
ID: 12555293
Thank you _Blue that really sounded like the answer... so i tried it but still getting page not found. It doesnt even goto the index.php page.

Any ideas?

Rob Holmes
0
 
LVL 20

Expert Comment

by:virmaior
ID: 12555660
I think it's a problem in what's happening before your rewrite rule gets implemented.
Many apache servers are designed to interpret "http://www.mydomain.com/products/subproduct/detail"
as "http://www.mydomain.com/products/subproduct/detail/"
they automatically add a trailing / and interpret an extensionless value as a directory

try adding a 404 rule to your htaccess
then have the php file it goes to  echo $_SERVER['REQUEST_URI']
if that does it, then you can't just use the rewriterule, instead set up an "error" document

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Assisted Solution

by:php-webdesign
php-webdesign earned 160 total points
ID: 12556001
make sure mod-rewrite is installed!!

<?
phpinfo();
?>

all the way down with "modules" there must be: "mod_rewrite"

put in the httpd.conf:

<Directory /www/htdocs/yourdomain>
AllowOverride All
</Directory>

htaccess:

RewriteEngine On
RewriteRule ^php/tutorials/(.*)/(.*)/(.*)/products/index.php?p=$1&var2=$2&var3=$3
RewriteRule ^php/tutorials/(.*)/(.*)/(.*)products/index.php?p=$1&var2=$2&var3=$3
RewriteRule ^php/tutorials/(.*)/(.*)/products/index.php?p=$1&var2=$2
RewriteRule ^php/tutorials/(.*)/(.*)products/index.php?p=$1&var2=$2
RewriteRule ^php/tutorials/(.*)/products/index.php?p=$1
RewriteRule ^php/tutorials/(.*)products/index.php?p=$1
0
 

Author Comment

by:robholmes83
ID: 12564393
Hi php-webdesign,

I've had a look at phpinfo() and there is no mod_rewrite installed.

I have no access to the server and i dont think the hosting company will implement it for me.

Do i have any other options but to just make the url's like this:

/index.php?p=product

Thanks,

Rob Holmes
0
 
LVL 5

Expert Comment

by:php-webdesign
ID: 12564758
no, when mod rewrite is not installed you cannot change the way you call on your page... sorry
(at least not that i know off)

But you can always ask them to install mod-rewrite (or find another hoster ;))
0
 
LVL 10

Accepted Solution

by:
eeBlueShadow earned 180 total points
ID: 12565717
Yes, you can, with just a little adjustment to your site.

See http://www.sitepoint.com/article/search-engine-friendly-urls for a detailed guide

_Blue
0
 
LVL 5

Expert Comment

by:php-webdesign
ID: 12565762
Nice :) didn't know that!
0
 
LVL 20

Assisted Solution

by:virmaior
virmaior earned 160 total points
ID: 12565772
php-webdesign... that's only mostly true
if you add an error document rule to htaccess, you can get the same sort of control (albiet not as efficiently):

e.g. I use this to protect some of the pages on my server (in particular to protect jpeg files):

.htaccess:

Order Allow,Deny
Deny from all

# To allow execution of cgi scripts in this directory uncomment next two lines
#AddHandler cgi-script .bat .exe .pl .cgi
Options -Indexes


ErrorDocument 403 /finder.php

<FilesMatch "\.(gif|png|pdf|jpg|jpeg|swf)$">
Allow from all
</FilesMatch>

----

since every attempt to access any  *.php   will come up as forbidden

they will always load the file finder.php:

<?php
echo $_SERVER['REQUEST_URI'];
        $file = $_SERVER["DOCUMENT_ROOT"] . $_SERVER["REQUEST_URI"];
        if (file_exists($file))
        {
              include_once($file);
        }
        else
        {
            include "error.php";
       }


?>

----

And there you go.  Now you just have to translate the REQUEST_URI to the path and load the appropriate fil.e
0
 

Author Comment

by:robholmes83
ID: 12567705
Thank you everyone who was involved with this problem!

I have decided to split the points as there wasn't really a complete solution, but the three guys eeBlueShadow, php-WebDesign and virmajor shared the points as they made major contributions to me making my decision as to what action to take next.

I decided to give eeBlueShadow the accepted answer as the link he has given has really helped me for the future.

Thanks guys!

Rob Holmes

PS speak to you soon because no doubt i'll be back soon.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question