homepage Welcome to WebmasterWorld Guest from 54.227.89.236
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Add query string with mod-rewrite
FrankinLA




msg:4593691
 9:38 pm on Jul 16, 2013 (gmt 0)

Hi,

I'm pretty new to this and I'm trying to pretty up a URL that I want to redirect with a query string parameter.

I would like: mysite.com/somedirectory/somevalue to redirect to: mysite.com/somefile.php?qstring=somevalue.

In the htaccess file in the root of my site I have:

RewriteEngine On
RewriteRule ^/somedirectory/(.*) /somefile.php?qstring=$1


Which looked like exactly what I wanted, but doesn't seem to work.

Any help would be greatly appreciated.

Thanks

FrankinLA

 

phranque




msg:4593694
 9:53 pm on Jul 16, 2013 (gmt 0)

welcome to WebmasterWorld, FrankinLA!


(external) redirect or (internal) rewrite?
in directory (or .htaccess) context the current directory is stripped so you will never match a leading slash (the root directory).

FrankinLA




msg:4593702
 10:07 pm on Jul 16, 2013 (gmt 0)

Thanks. Everything stays on my site, so I'm guessing that's internal?

The htaccess rewrite rule is in the root of mysite not somedirectory. Not exactly sure what your are asking. Thanks again.

phranque




msg:4593718
 11:17 pm on Jul 16, 2013 (gmt 0)

internal vs external refers to the server, not the site.
redirect means the server sends a 301 status code and a Location: header as a response.
rewrite means the server sends a 200 OK status code and the document at the internally rewritten url.

FrankinLA




msg:4593732
 11:54 pm on Jul 16, 2013 (gmt 0)

Thanks again. It sounds like I should have said rewrite and not redirect in my original post

When I use this:

RewriteRule ^/somedirectory/(.*) /somefile.php?qstring=$1

I get: The requested URL /somedirectory/somevalue was not found on this server.

lucy24




msg:4593757
 3:10 am on Jul 17, 2013 (gmt 0)

Let's try it a different way.

redirect = browser address bar changes.
rewrite = browser address bar stays the same, but server secretly fetches content from some other place

Given the two forms
example.com/somedirectory/somevalue
example.com/somefile.php?qstring=somevalue
let us please assume that what you want to do is:

User requests
/somedirectory/somevalue
and browser's address bar says so. But the content of the page comes from
/somefile.php?qstring=somevalue

When I use this:

RewriteRule ^/somedirectory/(.*) /somefile.php?qstring=$1

I get: The requested URL /somedirectory/somevalue was not found on this server.

That means the rewrite is not taking place. Is this happening in htaccess? If so, you MUST omit the leading directory slash, or the rule will always fail. Keep the opening anchor.

Can we assume for the sake of discussion that other RewriteRules work? If nothing works, you may have forgotten some essential boilerplate like RewriteEngine On. (Theoretically also "Options +FollowSymLinks", but I think almost all hosts switch this on in the config file if they're going to allow mod_rewrite in htaccess at all. I've never had to say it myself.)

And as long as we're in assuming-for-the-sake-of-discussion mode, let's also assume that your desired target somefile.php actually exists and has been programmed to await a qstring input ;)

Quick edit:
Is "somedirectory" always the same, so the target file doesn't have to do anything about that part?

phranque




msg:4593770
 4:19 am on Jul 17, 2013 (gmt 0)

I get: The requested URL /somedirectory/somevalue was not found on this server.

that means the RewriteRule didn't match your Pattern because the leading slash gets stripped in directory context (i.e. it's looking for a URI path of somedirectory/somevalue not /somedirectory/somevalue) and after failing that the server went into file path mode and attempt to locate a file name somevalue in a subdirectory of your document root directory named somedirectory.


try this:
RewriteEngine On
RewriteRule ^somedirectory/(.*) /somefile.php?qstring=$1



however your ultimate solution depends on how you answer lucy24's question:
Is "somedirectory" always the same, so the target file doesn't have to do anything about that part?

FrankinLA




msg:4593781
 5:29 am on Jul 17, 2013 (gmt 0)

Thank you both very much.

To Lucy24's question, yes I would prefer that the URL stays: example.com/somedirectory/somevalue and the content comes from:
example.com/somefile.php?qstring=somevalue

I do have both RewriteEngine On and the "Options +FollowSymLinks" in my htaccess file.

Yes, "somedirectory" is always the same and the only directory I wish to do the rewrite on. Because, I'm sure it matters, 'somefile.php' is not in "somedirectory" but one level back, although it could be in "somedirectory" if that mattered, or made it simpler.

lucy24




msg:4593823
 7:39 am on Jul 17, 2013 (gmt 0)

As your rule is currently written, the target file would be at the root:

/somefile.php?qstring=$1
=
www.example.com/somefile.php?qstring=$1

Since nobody will ever see it, you can put it anywhere that's convenient for you.

Oh yes and...
I get: The requested URL /somedirectory/somevalue was not found on this server.

Where do you get this? In the browser, in your error logs, somewhere else?

One of the tricky aspects of a rewrite-- as opposed to a redirect-- is that any user-visible error messages will be couched in terms of the URL you requested, not the location where the content really lives. So it is sometimes helpful for testing purposes to slap on an [R] flag. Then you'll see the browser's address bar changing, and you'll know that things are happening as intended. As soon as* you see it's working, yank away the [R] flag.


* Truism: If you detect and correct a mistake five minutes after you made it, and it's in a directory that search engines only visit once a week, their weekly visit will have taken place within that five-minute period. Lissen, bingbot, there's no such word as "innuuniq". It's a TYPO, all right?

phranque




msg:4593852
 10:07 am on Jul 17, 2013 (gmt 0)

along the lines of lucy24's post above, your complete solution might include an external (301) redirect such that a request for this:
http://example.com/somefile.php?qstring=somevalue
gets redirected to the canonical url:
http://example.com/somedirectory/somevalue

that way if your internal urls ever get exposed to a public visitor you won't be serving content from those internal urls.

g1smd




msg:4593876
 12:47 pm on Jul 17, 2013 (gmt 0)

RewriteRule pattern target flags

The pattern should match the requested path, without leading slash, as requested by the browser.

The target will point to an internal server resource without protocol or hostname in a rewrite, and to a new and different URL with protocol and hostname in a redirect.

The flags will be [L] for a rewrite or [R=301,L] for a redirect.

When redirecting a request for a URL with parameters to a new URL, you also need a preceding RewriteCond looking at THE_REQUEST in order to prevent an infinite redirect-rewrite-redirect-rewrite-... loop.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved