/    Sign up×
Community /Pin to ProfileBookmark

htaccess modrewrite / redirect Q

Hi, I’m not sure if this is the right place or not so if it’s in the worng area please move it.

Ok, I’m trying to redirect a page with parameters from one folder to another.

what I have going is site.com/folder/cgi-bin/folder/view?cmd=view&ID=22

I need that to go to site.com/page.php?id=22

EVERYTHING i have tried or found by searching gives an internal error (500)

I have sucessfully redirected
site.com/folder/cgi-bin/folder/page to site.com/page.php so I’m pretty sure it’s not the cgi-bin and more in the code I’m using. can I get some pointers or tips on how to redirect a variable like that?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@stephan_gerlachAug 21.2008 — try this one

[code=php]


Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^folder/cgi-bin/folder/view?cmd=view&ID=([0-9]+)$ page.php?id=$1



[/code]
Copy linkTweet thisAlerts:
@72newbieauthorAug 21.2008 — well at least it's not throwing a 500 error, now it's a 404. That a rewrite though, right? I'm needing to redirect the url b/c that folder no longer exists.

also can you explain, roughly what each step does?
Copy linkTweet thisAlerts:
@ibechaneAug 22.2008 — If I am correctly understanding what you're trying to do, then I think stephan has it backwards. It should be:

<i>
</i>RewriteEngine On
RewriteRule ^page.php?id=([0-9]+)$ /folder/cgi-bin/folder/view?cmd=view&amp;ID=$1


don't forget to escape the question mark, since that is a regex special character.
Copy linkTweet thisAlerts:
@ibechaneAug 22.2008 — I reread your post and I think I may have misunderstood your problem. Disregard my last post, and apologies to Stephan!

So, the way I understand it now is that you previously used cgi-bin/folder/view to handing whatever server-side scripting. But now, you've changed over to php and, using the same parameters, would like to use page.php to handle your scripting. In order to ensure that all remaining calls to cgi-bin/folder/view (possibly some that are out of your control) are handled, you would like to redirect all cgi-bin/folder/view requests to page.php just in case.

If that is the case, Stephan's code should do (barring the need to escape the question mark). I believe the reason you need to use RewriteEngine is that it allows you to make rules that use regex backreferences, which are needed to pass the data in the URL querystring.

If you want the redirect to be explicit (i.e. the url changes so that the clients know that they are being redirected), you can add a [R] option at the end of the rewrite line:

<i>
</i>RewriteRule ^folder/cgi-bin/folder/view?cmd=view&amp;ID=([0-9]+)$ page.php?id=$1 [R]


If this is not working, try adding a slash before page.php:

<i>
</i>RewriteRule ^folder/cgi-bin/folder/view?cmd=view&amp;ID=([0-9]+)$ /page.php?id=$1 [R]


Since your page.php is always going to be right after site.com/, specifying the root directory may remove some unforseen problems with a search and replace.
Copy linkTweet thisAlerts:
@72newbieauthorAug 24.2008 — I tried both of those and nothing happened, and a couple other things I could think of. my question is won't that allow a mod rewrite but not redirect?
Copy linkTweet thisAlerts:
@ibechaneAug 24.2008 — You should google mod_rewrite and rewriterule if you want to know more about that. Basically it tells your server to trick the client by sending them the data for page.php even though they asked for folder/cgi-bin/folder etc..

When you put the [R] at the end, it makes it also redirect by explicitly changing the browser url to the new one. Some people like to leave this flag out if they want to hide the true file structure of their server.

Anyway, I figured out what was wrong. Apparently rewriterule does not look at the query string, so you have to use rewrite conditions to match the url/uri and the query string separately:

[CODE]ReWriteEngine On
ReWriteCond %{REQUEST_URI} ^/folder/cgi-bin/folder/view$
RewriteCond %{QUERY_STRING} ^cmd=view&ID=([0-9]+)$
RewriteRule ^.*$ /page.php?ID=%1 [R][/CODE]


Hopefully that one should work. The %1 allows for backreferencing a condition.
Copy linkTweet thisAlerts:
@72newbieauthorAug 24.2008 — wow, that works great. I take it the ^.*$ is somehow saying this domain? it seems like it shouldn't need to reference that?
Copy linkTweet thisAlerts:
@ibechaneAug 24.2008 — ^.*$ is regex search string that basically means "anything or nothing". I'm not really sure how that search string is applied since I put the request_uri condition there. As far as I know, this bypasses the rewriterule's search and just uses the request_uri one.
Copy linkTweet thisAlerts:
@ayveghAug 27.2008 — Here's a helpful wizard:

http://www.mod-rewrite-wizard.com/
×

Success!

Help @72newbie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...