Solved

htaccess rewrite for get variables

Posted on 2009-05-08
7
1,373 Views
Last Modified: 2012-05-06
I see a lot posted on this but I am going crazy trying to get this to work on my site and I know it is simple.  I have a simple site at www.fostersolutionsinc.com.  I the attached code in my htaccess file (not including the two lines after the space).  It has worked fine to strip the .php and give me friendly links, but now I need to add a get variable.  

I thought that the rewrite rules would have worked to take "subdirectories" and create them as php get variables.  I added the two lines after the space as a way to specifically create the $client variable, but it doesn't seem to be working.  When I go to http://www.fostersolutionsinc.com/portfolio/hello, as a test, it shows up but without the css, or something.  I'm not sure what is going on.

What am I missing?
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} \.(js|gif|jpg|png|css|htm|html)$
RewriteRule ^(.*)$ $1
RewriteRule ^(index\.(html?¦php))?$ /index.php? [QSA,L]
RewriteRule ^([a-z0-9_-]+)\?(.*)$ /$1.php?$2
RewriteRule ^([a-z0-9_-]+)$ /$1.php
 
 
RewriteRule ^portfolio/?$ portfolio.php
RewriteRule ^portfolio/([-a-zA-Z0-9_]+)/?$ portfolio.php?client=$1 [QSA]

Open in new window

0
Comment
Question by:dougfosterNYC
  • 4
  • 3
7 Comments
 
LVL 27

Expert Comment

by:caterham_www
ID: 24337774
> RewriteRule ^(.*)$ $1

That doesn't make sense, it creates unnecessary overhead. Use the dash (no substitution) instead of a rewrite. And use the L flag, too. You won't need the cond. as well since you can check the extensions in the rule-pattern, which is being processed before your cond. is evaluated

> as a test, it shows up but without the css, or something.  I'm not sure what is going on.

your paths in your html source are wrong. If you use a folder structure, you must adjust your html references, too. Otherwise the browser will request resources from places where they aren't. A simple way would be to put a
<base href="http://example.com/" />
into your html <head> section or make sure the references are absolute, i.e. start with a leading slash (src="/foo.jpg" instad of src="foo.jpg").
RewriteEngine on
RewriteRule \.(js|gif|jpg|png|css|htm|html)$ - [L]
# what are you trying to achieve with this rule? Ereasing the queryString with a ? at the end plus qsa flag doesn't make sense.
# note that the queryString does not match the rule-pattern, it's passed on automatically to your substitution.
RewriteRule ^(index\.(html?&#166;php))?$ /index.php? [QSA,L]
RewriteRule ^portfolio/([-a-zA-Z0-9_]+)/?$ portfolio.php?client=$1 [QSA,L]
RewriteRule ^([a-z0-9_-]+)/?$ /$1.php [L]

Open in new window

0
 

Author Comment

by:dougfosterNYC
ID: 24338337
Hi caterham.

Well, clearly I  am not a unix person, so I'm only guessing here.  I changed my htaccess to what you put, and added the base href.  The base href did fix the problem of the links going to a non-existent subdirectory.

But it still isn't creating the client URL variable (see below).  I would think that if I go to http://www.fostersolutionsinc.com/portfolio/test, it would convert to  portfolio.php?client=test.  

		if (isset($_GET["client"]))
		{
			echo ($_GET["client"]);
		} else {
			echo "No client variable";
		}

Open in new window

0
 
LVL 27

Expert Comment

by:caterham_www
ID: 24338419
Do you have MultiViews enabled (may be in your server config)? In that case mod_negotiation will map portfolio/foo to portfolio.php prior mod_rewrite acts in per-directory context. You can disable MultiViews via

Options -MultiViews

if you're allowed to do so in .htaccess files.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:dougfosterNYC
ID: 24338727
I use a shared hosting environment.  When I added that line it blew up.

I am trying to get the host company to help me.  This seems more difficult than it should be....
0
 
LVL 27

Accepted Solution

by:
caterham_www earned 500 total points
ID: 24338924
If multiViews are part of the problem, you cold try to reword the pattern/URI path you're requesting. E.g. request

/p/test

with
RewriteEngine on
RewriteRule \.(js|gif|jpg|png|css|htm|html)$ - [L]
# what are you trying to achieve with this rule? Ereasing the queryString with a ? at the end plus qsa flag doesn't make sense.
# note that the queryString does not match the rule-pattern, it's passed on automatically to your substitution.
RewriteRule ^(index\.(html?&#166;php))?$ /index.php? [QSA,L]
RewriteRule ^p/([-a-zA-Z0-9_]+)/?$ portfolio.php?client=$1 [QSA,L]
RewriteRule ^([a-z0-9_-]+)/?$ /$1.php [L]

Open in new window

0
 

Author Comment

by:dougfosterNYC
ID: 24339222
Ok, that did the trick.  I wish I knew exactly  what is going on, but it worked. I  actually changed the php file to portfoliolist.php, and then I could keep the directory /portfolio/test.

I guess the "directory" name conflicted with the php file name.  I'll award the points, but if you could give a quick explanation on what is going on, that woudl be great.

Thanks.
0
 
LVL 27

Expert Comment

by:caterham_www
ID: 24339593
> I guess the "directory" name conflicted with the php file name.

Exactly. There's a feature (MultiViews) which finds a corresponding file if you request the file without its extension. MultiViews has other features depending on the http accept headers as well (language detection to map foo.html to foo.html.en etc.)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 40
apache and php 3 109
Stream live video from Raspberry Pi camera 22 149
Cpanel file manager 8 21
As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now