homepage Welcome to WebmasterWorld Guest from
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

Cant edit GET Form to using RewriteRule

 1:21 am on Mar 29, 2012 (gmt 0)

Hi ;-)
I'm using this rule:
RewriteRule ^([a-z]+)/?((\d+))?/?$ index.php?page=$1&id=$3 [QSA,L]
So it must be something like...
and for search
so... i cant make the GET form... to make that kind of action...
When i post in action parameter: action="/search/" my form open this:
i try to remove the name parameter but when submit... the form open this:
So.. any ideas how to fix that?
Thanks ;-)



 3:49 pm on Apr 5, 2012 (gmt 0)

Please, help me :/


 6:32 pm on Apr 5, 2012 (gmt 0)

Oi! You're not drowning, you're just asking an Apache question. Most people around here have day jobs. So let me start by ripping your rule to shreds.

RewriteRule ^([a-z]+)/?((\d+))?/?$ index.php?page=$1&id=$3 [QSA,L]

Rule works on any request that begins with one or more lower-case letters, possibly followed by some combination of numbers and directory slashes:


I'm pretty sure you didn't mean to admit all of those possibilities.

But first: I am mystified by the ((\d+))? element. Since $2 and $3 will always be the same, there is no need to capture them separately-- especially since you never use $2 at all! And since the whole thing is optional, it all collapses to (\d*)

I suspect what you really mean is


where the whole numerals directory is optional.

Now, the flags: [QSA] means "add this to the existing query" instead of the default "replace the existing query with this new one". But there shouldn't be an existing query. If there is no query, QSA will not do any harm. But it isn't necessary.

and for search

so... i cant make the GET form... to make that kind of action...
When i post in action parameter: action="/search/" my form open this:

i try to remove the name parameter but when submit... the form open this:

I think we may have a little bit of a language problem. What do you mean by "search+word"? Where is the "name parameter"? I don't mean where is it in your form. I mean, where is it in the RewriteRule?

You have probably made your Rewrite too general. Collapsing many similar things into a single rule is good, but you can overdo it. Showing a page is one thing; going to search results is another. I think it would be safer to make one Rule specifically for /search/ and a different rule for all the other things.


 5:07 pm on Apr 21, 2012 (gmt 0)

I do other thing... I edit my rewriterule because i need another parameter... &view...
RewriteRule ^([a-z]+)/?([a-zA-Z0-9+_-]+)?/?((\d+))?/?$ index.php?page=$1&id=$2&view=$3 [QSA,L]

And for the Search form i use this in index.php page:
$page = $_SERVER['REQUEST_URI'];
if (preg_match('|/\?page=search&id=([a-zA-Z0-9-_+]+)|i', $page)) {
preg_match('|/\?page=search&id=([a-zA-Z0-9-_+]+)|i', $page, $exitstr);
$page = '/search/'.$exitstr[1].'/';
exit(header('Location: '.$page));

It works really well.... but... sometime i have few filters for search (3 selects)
and sometime the search query will be

but not every time... this code works only when cat country and player parameters are missing.. but if they are there... its not working... I change the code... and made it to work with the parameters... but than... isnt work if they are missing :/
Its so complicated... i guess you understand me... if not... i'll try to explain better.


 12:40 am on Apr 22, 2012 (gmt 0)

Do a forums search for "query string" + "boilerplate". It was most recently posted just a few days ago.


 12:29 am on Apr 23, 2012 (gmt 0)

I search what you give me to search... but i dont found something that i can use, so.. i work with the preg_match code
I made this preg_match:
for the code that i post in my last coment.

So... its working if the url is like:

But NOT work only if the urls is:

I try everything with "?" char but... nothing work...
Can you help only for this


 2:51 am on Apr 23, 2012 (gmt 0)

I think you're trying to deal with a mod_rewrite problem and a php problem at the same time. This is, haha, a problem.

But there is one thing all Regular Expressions have in common. A construction like this


can only lead to disaster. Even in an ordinary URL it makes trouble for the server, because it has to stop and start and backtrack and try again and capture and throw away a capture and try something else and backtrack a little further... and so on, while your customers grow old.

Here you have a series of .* that are not constrained in any way. That means it could capture (I added spaces to make it wrap)

&player=Smith &hobby=fishing &shopping=GreatWidgets &country=Cacklogallinia &favoritecolor=turquoise &favoritewebsite=www.example.com &bestspammycomment=### &cat=Fluffy &othercat=Fritz &id={... and here finally is the format every capture should have had}

or it could capture


The parts in boldface have to be present. All the other stuff can be present whether you want it there or not-- or it can be absent even if you need it.

There is a recent thread talking about putting all the pieces of an URL into the same order every time. In a query it doesn't matter so much. Write your code so that it captures each query individually, regardless of what comes before or after it.


 11:00 am on Apr 23, 2012 (gmt 0)

This .* i will replace with d+ once i run the script correctly :)
The code is that:
$page = $_SERVER['REQUEST_URI'];
if (preg_match('|/\?page=search?&player=(.*)?&country=(.*)?&cat=(.*)?&id=([a-zA-Z0-9-_+]+)?|i', $page)) {
preg_match('|/\?page=search?&player=(.*)?&country=(.*)?&cat=(.*)?&id=([a-zA-Z0-9-_+]+)?|i', $page, $exit);
$page = '/search/'.$exit[4].','.$exit[1].','.$exit[2].','.$exit[3].'/';
exit(header('Location: '.$page));
and once if found in url like "?page=search&cat=1&country=2&player=3&id=Search+word"
it redirects to /search/Search+word,1,2,3/
I dont think there is a problem for loading the site. And also i make another new rewrite rule, to read this numbers after searched keyword.

But only can make the preg_match, to redirects correctly, because i have 2 search forms. One of the top of site, that is in all pages, and one on the search page with filters (cat, player, country). And the code inst work if the page is


 9:46 pm on Apr 23, 2012 (gmt 0)

Ok, now i made 2 preg_match...
$page = $_SERVER['REQUEST_URI'];
if (preg_match('|/\?page=search?&player=(.*)?&country=(.*)?&cat=(.*)?&id=([a-zA-Z0-9-_+]+)?|i', $page)) {
preg_match('|/\?page=search?&player=(.*)?&country=(.*)?&cat=(.*)?&id=([a-zA-Z0-9-_+]+)?|i', $page, $exit);
$page = '/search/'.$exit[4].','.$exit[1].','.$exit[2].','.$exit[3].'/';
exit(header('Location: '.$page));
if (preg_match('|/\?page=search&id=([a-zA-Z0-9-_+]+)?|i', $page)) {
preg_match('|/\?page=search&id=([a-zA-Z0-9-_+]+)?|i', $page, $exit);
$page = '/search/'.$exit[1].'/';
exit(header('Location: '.$page));

and now it works how i want it... but still want to make it with one preg_match


 3:24 am on Apr 24, 2012 (gmt 0)

You mean, you'd like to consolidate the two possibilities




Does the "id" element have to come at the end of the query string? Can you fiddle with the code so "id" comes first, whether or not there are other queries? Seems like that would solve a few problems.


What's that ? at the end of "search" in the four-term version? I can deal with everything else.

Did I mention that I don't speak a word of php? I'm always frabjazzled by its fondness for every possible kind of bracket. But stare at it long enough and it starts making approximate sense. I'm assuming what we've got here is the rough equivalent of javascript

expr = /{blahblah}/gm;
output = output.replace(expr,"");

with a preliminary "if" test thrown in for good measure.


 10:56 am on Apr 24, 2012 (gmt 0)

- For first question: Yes, because heave 2 search forms, one with filters (player countru cat) and one without...
- Second: i cant move the id first... or i dont know how. Maybe i must move the filters after keyword input.. but its not good view :)
- The ? in the end of "search" is because, when you use filters, you can use only them, and keywords may be missing... I really dont know where to put this ? so i test few place, and there works that i want it :D
For the end of the post, i cant understand you for that javascript.


 11:44 pm on Apr 24, 2012 (gmt 0)

You mean it works if the word "search" is entirely missing? By ordinary RegEx rules I would expect it to recognize the misspelling "searc" since the ? comes after a single letter.


 1:30 pm on Apr 26, 2012 (gmt 0)

i dont understand this RegEx rules, i only try something, and if it works is ok for me. For that i want help from here :)

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