Jump to content

Recommended Posts

Hello all,

 

I currently have a form that posts variables leaving the URL looking like

4cache.net/search.php?q=cat

I have a couple of mod_rewrite rules that allow it to look like

4cache.net/search/cat.html

 

My problem is, when the form is processed the URL shows up as the ugly method, is there any way to make it so that on submission of the form it Posts to the pretty URL and not the ugly one?

 

The Form itself

print "<form name=\"search\" action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"get\">";
print "<div>Enter Search Term: <input type=\"text\" name=\"q\" value=\"$term\" />";
print "<input type=\"submit\" /></div>";
print "</form>";

 

The mod_rewrite rules I currently have in place

#point /search/<search>.html here with some pretty page numbers too
RewriteRule ^search$ /search.php [L,NC]
RewriteRule ^search/$ /search.php [L,NC]
RewriteRule ^search/(.*)\.html$ /search.php?q=$1&%{QUERY_STRING} [L,NC]
RewriteRule ^search/(.*)\.html,([0-9]*) /search.php?q=$1&pagenum=$2 [L,NC]

Link to comment
https://forums.phpfreaks.com/topic/185409-pretty-urls-and-forms/
Share on other sites

I guess you could do something along the lines of...

 

RewriteRule ^search.php?q=([a-z0-9]+)$ /search/$1.html [R]

 

...you would need to place it before your other rule. The basic idea is it will redirect the 'ugly' URL to the 'pretty' URL, changing it in the users browser, then re-direct back to the 'ugly' URL but without actually changing the users address bar.

 

Note: untested.

You cannot do that. RewriteRule doesn't match against the query string, but only the path.

 

RewriteCond %{QUERY_STRING} q=([^&]+)
RewriteRule ^search.php$ /search/%1.html [R]

 

Likewise untested.

 

 

Also, your existing rewrites could be written like this instead:

RewriteRule ^search/?$ /search.php [L,NC]
RewriteRule ^search/(.*)\.html$ /search.php?q=$1 [L,NC,QSA]
RewriteRule ^search/(.*)\.html,([0-9]+) /search.php?q=$1&pagenum=$2 [L,NC]

In total, try making it this (in this order):

 

RewriteRule ^search/?$ /search.php [L,NC]
RewriteRule ^search/(.*)\.html$ /search.php?q=$1 [L,NC,QSA]
RewriteRule ^search/(.*)\.html,([0-9]+) /search.php?q=$1&pagenum=$2 [L,NC]

RewriteCond %{QUERY_STRING} q=([^&]+)
RewriteRule ^search.php$ /search/%1.html [R,L]

I found this code on another forum:

 

rewritecond %{query_string} &?file=([^&]+) [NC] 
RewriteRule ^photos/watermark\.php$ /photos/data/%1? [NC,L] 

 

And I've adapted it into my own. We no longer have an infinite loop but we are now getting the URL looking like 4cache.net/cat.html?q=cat

 

RewriteRule ^search/?$ /search.php [L,NC]
RewriteRule ^search/(.*)\.html$ /search.php?q=$1 [L,NC,QSA]
RewriteRule ^search/(.*)\.html,([0-9]+) /search.php?q=$1&pagenum=$2 [L,NC]

RewriteCond %{QUERY_STRING} q=([^&]+)
RewriteRule ^search.php$ /search/%1.html [R,L]

I sorted it out the other way. This was added to the top of search.php

$script = $_SERVER['REQUEST_URI'];
list($domain,$query) = split("=",$script);
$location = "http://4cache.net/search/$query.html";
if($domain == "/search.php?q")
{
  header('Location: ' . $location);
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.