Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod rewrite with multiple queries



9:16 pm on Feb 24, 2014 (gmt 0)

Hi all,

I have been working on what I thought was a simple re-write rule. I feel I'm like 90% at a solution, but am missing the final piece of the puzzle.

The premise is:

I have a URL a user clicks on. They all look like this:

In order to force a login page and pass them back to the original page requested, I need to rewrite the URL to look like this:

Right now, this is what I have:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC]
RewriteRule (.*) [sub.domain.com...] [QSA,R=301,L]

I'm ignoring file names so images/css appear as expected. That's working. The URL rewrites and displays in the browser as: [sub.domain.com...] - note the & in place of the ? in the original URL

I think I need to change all of the characters (/ = ? and &) for everything after the "o=" using URL encoding. But I was hoping to not get into something that complex. Is there anything I'm missing? I've seen code examples for all sorts of things and read documentation, but something is not clicking for me. Any guidance in the right direction would be greatly appreciated.

[edited by: phranque at 10:35 pm (utc) on Feb 24, 2014]
[edit reason] disabled graphic smileys [/edit]


10:02 pm on Feb 24, 2014 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

That code is not a rewrite. That's a redirect.

RewriteRule can be configured to do either.

RewriteRule cannot make URLs nor can it change URLs. It either suggests the browser makes a new request for a different URL. That's a redirect. Or, upon a URL request being received by the server, mod_rewrite can alter the internal file system pointer to look at a different place in the filesystem to that suggested by the path and file part of the original URL request. That's a rewrite.

URLs are defined/created in the href part of links.

According to your rule, once logged in the user will always be redirected by your rule whenever they request the original URL again. The rule has no way to redirect some requests and not others. All users are redirected. Surely, detecting login status is something the PHP script should be doing? PHP can issue redirects by using the HEADER directive.

[edited by: g1smd at 10:28 pm (utc) on Feb 24, 2014]


10:06 pm on Feb 24, 2014 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

Before you go there...

I need to rewrite the URL to look like this:
Why? Wouldn't it be simpler to have a normal query with ampersand in the middle, and let the php sort it out?

If it has to be a question mark, you can't use QSA. You'd have to

:: detour to test site, because something very similar came up recently ::

RewriteCond %{QUERY_STRING} !^o=
RewriteCond %{QUERY_STRING} (.+)
RewriteRule (.*) http://www.example.com/pagename.php?o=$1?%1 [R=301,L,NE]

You do not want to do this. The first condition is to prevent an infinite loop. The last condition-- which has to be last-- is the one you're capturing from.


1:54 pm on Feb 25, 2014 (gmt 0)

So, I guess a little background is needed. First off, thank you for the responses. I'm not usually one to noob around on a forum, but I was/am running out of options.

The link the user clicks on goes to a page inside our web app. Let's say our normal domain is app.domain.com. These links go out as [app.domain.com...]

At that point the web app does detect login status and sends the user to the login page with the o=dir/page.jsp?pageId=1234567&id=9876 (the browser displays the URL encoded ?, =, &). The o variable is stored, then used to take them to the originally requested page. Works fine.

What the app allows us to do is create a custom login page for individual clients. This login page is an html page that passes the authentication along while giving them a customized look. So that's my sub.domain.com/index.html page from my first post. The links to a page+id are auto-generated by the system. By default they go to app.domain.com, but can be changed to sub.domain.com and using the o= variable via that custom login page pass the user along to the proper location after login. Yes, using a redirect I am able to give the custom login page, but I really need the user to head directly to the page they originally requested. As of now they are redirected to the login page and can auth fine, but go directly into the main page of the app after login. That's the full background of my problem.

I guess also assume I can't change the custom login page outside of it grabbing that o= variable.

[edited by: phranque at 8:32 pm (utc) on Feb 25, 2014]
[edit reason] disable graphic smileys [/edit]


2:10 pm on Feb 25, 2014 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Sounds like the app should itself generate the redirect after login.

Doesn't sound like something that mod_rewrite can help you with.

Featured Threads

Hot Threads This Week

Hot Threads This Month