Forum Moderators: phranque

Message Too Old, No Replies

Problem in redirection through .htaccess

"Browser is not redirecting correctly"

         

jribeiro

3:02 pm on Sep 7, 2011 (gmt 0)

10+ Year Member



Hi there!

I have a php generated file like:
http://www.domain.com/?mod=test&act=view


And I want to create a redirection from that address to something like:
http://www.domain.com/view-test


so that everytime a user (or bot) accesses the first uri it gets redirected to
http://www.domain.com/view-test
viewing the content of the first uri.

Right now I have the following rules under my .htaccess:
RewriteRule ^view-test$ /?mod=test&act=view [NC] 
RewriteCond %{QUERY_STRING} mod=test&act=view
RewriteRule ^(.*)$ /view-test? [R,L]


The first rule creates a "page alias" and works if I delete the other two lines (but doesn't redirect my users as i want to)

After placing the last two rules I end up in a Loop or something and I get a broswer message saying "The page is not redirecting correctly"... Any idea what I'm doing wrong?

rocknbil

4:00 pm on Sep 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard jribeiro, you can see the loop right there - you capture view, redirect to query string version, then back to view.

What is your goal? If you put **this** in your pages and your output,

/view-test

Then do this,

RewriteRule ^view-test$ /?mod=test&act=view [NC]

It should rewrite to whatever's behind ? (presuming index.php) yet the address bar will remain at "/view-test".

jribeiro

4:08 pm on Sep 7, 2011 (gmt 0)

10+ Year Member



Hi thanks for the reply!

Yes that works as you say. The url shows /view-test and the content from ?mod=test&act=view is loaded.

My problem is inverse I think. Besides that (which is correct) I also want users to be redirected to url /view.test when they access the original url: ?mod=view&act=test

This is because all my urls in the web application are in the original form (/?mod=view&act=test) and I don't want to change them all to the new user friendly format.

Sorry if I didn't make myself clear the first time around... Hope I managed it now ;)

Thanks once again!

lucy24

8:48 pm on Sep 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This seems to be the Question of the Week, so thumbing through recent threads in this forum should get you multiple answers. That is, multiple variations on the same answer.

You want the user's address bar to show A, while they are really getting content from B. That's a Rewrite. But for those users who actually ask for B, you want them to think they are at A. That's a Redirect (flag [R=301]).

:: search, search ::

Here's one answer [webmasterworld.com] from a Knowledgeable Source. (Not me, in other words.) In brief: you look at {THE_REQUEST} to grab only those requests coming in from outside, and redirect them to the displayed location if necessary. Then you take everyone and rewrite them back to the real location.

g1smd

9:24 pm on Sep 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Redirect using a RewriteRule but with a preceding RewriteCond looking at %{THE_REQUEST} to ensure that only external requests (not previously internally rewritten requests) are redirected.

Rewrite using the simple RewriteRule you had before.

There's over 1000 previous threads with code and comments. Read any 20 of them and you will know everything there is to know about this subject.

The major breakthrough comes when you realise that a redirect maps a URL to another URL (URLs are used "out there" on the web) and that a rewrite maps a URL to a filepath (filepaths are used here "inside" the server). Filepaths and URLs are not at all the same thing, they are merely linked by the actions of the server.

The general format is:

# REDIRECT
RewriteCond %{THE_REQUEST} [A-Z]{3,9}\ /<pattern1>\ HTTP/
RewriteRule ^<pattern2> http://www.example.com/<somepath> [R=301,L]


# REWRITE
RewriteRule ^<pattern3> /index.php?query=<value> [L]

jribeiro

12:27 pm on Sep 9, 2011 (gmt 0)

10+ Year Member



Hi all! Thanks for the replies...

After reading a lot and go through your suggestions I came up with this rule:
RewriteCond %{QUERY_STRING} mod=test&act=view
RewriteRule ^$ view-test? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^view-test.html amas/index.php?mod=test&act=view [L]

The problem (and a big one) is that all url parameters or Form submission data (both through POST or GET) is lost after applying this rule so basically nothing works in my PHP website.

How can I solve this?! Is it even possible to achieve what I want?

Thanks

lucy24

6:05 pm on Sep 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The parameters are getting lost because you are telling them to get lost. That is, you are replacing the original query string with mod=test&act=view.

:: shuffling papers ::

Changing a Query

#1 To delete a query, add a ? to the end of your rewrite target.
#2 To replace a query—or create a new one—add ?blahblah to the rewrite target. The blahblah can be either literal text, or stuff you captured earlier. (#1 and #2 are really the same thing: you're just replacing the query with either something or nothing.)
#3 To add to an existing query, again put ?blahblah at the end of the target, but also add [QSA] to your flags (the bracketed items at the end of the Rule). It stands for "Query String Append", meaning that the blahblah is to be added to the existing query—if any—instead of replacing it.

You can find the full boilerplate in a few random threads hereabouts, but that's the part you need to internalize.

jribeiro

6:53 pm on Sep 9, 2011 (gmt 0)

10+ Year Member



Yey!

Thanks again! I've tried that... Added the QSA flag but the problem is that I get the following URL:
[domain.com...]

It doen't make sence to have view-test and the name of mod and act upfront... Also I didn't want to have to match all the possible parameters 'cause there's too many at the real url... :/

lucy24

8:35 pm on Sep 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you want the new query to come at the end of the old query, you would first have to capture the entire old query. Then, instead of QSA, you'd have

{the beginning of the url}?%1&mod=test&act=view

But this can rapidly get you into an icky mess. It is probably less trouble to write your php or equivalent so it deals with each piece of the query wherever it finds it. That's why query strings-- and analogous things like cookies-- come in the form {foo}={bar}. Deal with {foo}, not with "the third query in the list". And if there aren't too many of them, it may really be less trouble to ignore unused parameters rather than go to the trouble of hunting them down and eradicating them.

Riddle for the ages >> We carried away all that we did not catch, and all that we caught we left behind.

jribeiro

9:35 pm on Sep 9, 2011 (gmt 0)

10+ Year Member



My code handles the extra parameters OK. They find it no matter the position. My problem is not the code but just understanding where I'm at in htaccess (cause it's the first time I'm trying to achieve this) ;)

The problem is that I find an url like:
http://www.domain.com/view-test?mod=test&act=view&order_by=id


a bit "dumb"... It translate the url to a friendly one but then add up all the old url... For this I would stick with the non-friendly url, no? :D

Thanks for the time and patience

g1smd

11:28 pm on Sep 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It looks like you didn't read my earlier post.