Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

mod_rewrite - how to test it? Best practices?

Hi,

I read this

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_22526557.html

and added
.htaccess with
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/([^/]+)(/)?$ $1\.php?type=$2&model=$3 [nc]

to my directory smart:

http://www.myserver/smart

in the same dir I added cars.php

<?php

$self_url = sprintf('http%s://%s%s',
  (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == TRUE ? 's': ''),
  $_SERVER['HTTP_HOST'],
  $_SERVER['REQUEST_URI']
);

echo "[".$self_url."]";

?>

If I do

http://www.myserver/smart

I am told to add an index.html - that is understandable
If I do

http://www.myserver/smart/cars.php
I see
[http://www.myserver/smart/cars.php]
and if I do

http://www.myserver/smart/cars.php/ford
I see
[http://www.myserver/smart/cars.php/ford]

How do I see if my rewrite rule works and how do I get to do

http://www.myserver/smart/parm1/parm2

or

http://www.myserver/smart/name1/value1

so I can execute

cars.php?parm1/parm2
or perhaps
cars.php?name1=value1

And can I have a rewrite rule and a php in smart so I can do

http://www.myserver/smart/parm1
to execute
http://www.myserver/smart/cars.php?parameter=parm1


Thanks

Michel







0
Michel Plungjan
Asked:
Michel Plungjan
  • 19
  • 13
1 Solution
 
ellandrdCommented:
Hi Michel

your rewrite rule will work if your url looks like this:

http://site.com/cars/parm1/parm2/

because you have called your PHP file cars.php your rewrite rule is looking for smart.php.

rename your cars.php file to smart.php and it will work.

This is clear?
0
 
ellandrdCommented:
Basically the $1 equal the first value in the rule, $2, the second value and so on.

decoded: RewriteRule ^(smart)/(ford)/(kia)(/)?$ smart\.php?type=ford&model=kia [nc]

Hope this makes more sense - its hard to explain.

Ellandrd


0
 
Michel PlungjanIT ExpertAuthor Commented:
Ahh..

What I want is not to have smart.php in the root but to have
whatever.php in sub directory smart and pass all requests to
/smart/??????
to
/smart/whatever.php?parm=?????

I do not dare to rewrite the root .htaccess hence the subdir

Thanks

Michel
0
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.

 
ellandrdCommented:
if your dont want to write the root .htaccess and you want to have your whatever.php in a sub directory, then you would have this in your .htaccess file (root .htaccess file)

RewriteEngine on
RewriteRule ^smart/([^/]+)/([^/]+)(/)?$ smart/$1\.php?param=$2 [nc]

so an example for "/smart/??????" would be:

http://site.com/smart/cars/ and this would rewrite to:

http://site.com/smart/whatever.php?param=cars

>>I do not dare to rewrite the root .htaccess hence the subdir

why not?  
0
 
ellandrdCommented:
actually that is wrong. here is the correct rewrite rule for http://site.com/smart/cars/ :

RewriteEngine on
RewriteRule ^smart/([^/]+)(/)?$ smart/whatever.php?param=$1 [nc]

If your URL was like http://site.com/smart/cars/ford/ and your PHP file was called cars you would have this:

RewriteEngine on
RewriteRule ^smart/([^/]+)/([^/]+)(/)?$ smart/$1\.php?param=$2 [nc]

Ellandrd
0
 
Michel PlungjanIT ExpertAuthor Commented:
If I mess the rewrite rule I can turn the site off, no?

thanks for the examples. I will try

Michel
0
 
Michel PlungjanIT ExpertAuthor Commented:
Can I have a rewrite rule in root that given
http://site.com/smart/cars/ford/ 

calls

http://site.com/smart/whatever.php?cars=ford
?
0
 
ellandrdCommented:
turn the site off?  if your rule is wrong, then your users will get a 404 page not found or get redirected to another page depending on how your .htaccess file/server is setup.

ellandrd
0
 
ellandrdCommented:
yes:

RewriteEngine on
RewriteRule ^smart/([^/]+)/([^/]+)(/)?$ smart/whatever.php?$1=$2 [nc]
0
 
ellandrdCommented:
0
 
Michel PlungjanIT ExpertAuthor Commented:
that is what I meant - if I get it wrong then ALL urls are rewritten I guess
0
 
ellandrdCommented:
No - only that wrong URL will not work.  the rest of the rules will still work fine.

0
 
Michel PlungjanIT ExpertAuthor Commented:
Does it need a reload of the server? I cannot seem to get it to work...
0
 
ellandrdCommented:
it shouldnt, but you can restart apache if you want...

when you say it doesnt work - what page appears (if any)
0
 
ellandrdCommented:
0
 
Michel PlungjanIT ExpertAuthor Commented:
The .htaccess in your root?

Cool.

Great

Thanks for the example.

0
 
ellandrdCommented:
yes my folder structure is:

w:/apache/htdocs/ee/.htaccess
w:/apache/htdocs/ee/smart/whatever.php
0
 
Michel PlungjanIT ExpertAuthor Commented:
How do you do that when your dir is /ee/  ???

Thanks
0
 
Michel PlungjanIT ExpertAuthor Commented:
Ahh, so the .htaccess is just one higher
0
 
ellandrdCommented:
Yes - I like to keep all EE examples together.
0
 
ellandrdCommented:
the .htaccess can exist in any folder so if i was to place it under:

w:/apache/htdocs/.htaccess

and have smart with whatever.php in my EE folder like this:

w:/apache/htdocs/ee/smart/whatever.php

then i would have my rewrite rule like this :

RewriteRule ^ee/smart/([^/]+)/([^/]+)(/)?$ ee/smart/whatever.php?type=$1&model=$2 [nc]
0
 
Michel PlungjanIT ExpertAuthor Commented:
Ok.

I have

http://www.<virtualhost>.com/smart/car/ford

and I get a 404 on

/home/com/<virtualhost>/html/smart/whatever.php

Better but not what I expected

I guess the .htaccess should NOT go in the root but in

/home/com/<virtualhost>/
or
/home/com/<virtualhost>/html

right?
0
 
ellandrdCommented:
I would put it in

/home/com/<virtualhost>/html

Let me see your rewrite rule aswell...
0
 
ellandrdCommented:
Where is the main index.html or default.html page exist for this virtual host?

does it exist under:

/home/com/<virtualhost>/html/index.html ??
0
 
Michel PlungjanIT ExpertAuthor Commented:
I am told

/<virtualhost>.com/html/index.html


which confuses me even more
0
 
ellandrdCommented:
Eh?  It is a confusing file structure...

OK so it looks like all the main files for the virtual host exist under:

/html/

so i would have:

/home/com/virtualhost/html/.htaccess
/home/com/virtualhost/html/smart/whatever.php



On my web server (Unix and apache) I have my virtual hosts setup like this:

/public_html/virtualhost1/.htaccess, .php etc
/public_html/virtualhost2/.htaccess, .php etc

for my seandelaney.co.uk files,my main domain - they are located under:

/public_html/.htaccess
/public_html/index.php etc etc
0
 
ellandrdCommented:
how are you getting on Michel?

ellandrd
0
 
Michel PlungjanIT ExpertAuthor Commented:
Not at all :(((

I was wondering if you could email me so I could give you the site details?

My address is in my profile

Thanks
0
 
ellandrdCommented:
Cheers Michel

I had forgotten about this question..

Thanks for the grade

Ellandrd
0
 
Michel PlungjanIT ExpertAuthor Commented:
Yeah - it worked like you said when we sorted out where to put it...
Thanks to you too...

Now I will try to set up a mySql DB to test searching fields
0
 
ellandrdCommented:
I'm surprised that you are not most experienced with PHP/MySQL...
0
 
Michel PlungjanIT ExpertAuthor Commented:
My weapon of choice is JavaScript :)
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 19
  • 13
Tackle projects and never again get stuck behind a technical roadblock.
Join Now