lankyzor Posted December 16, 2009 Share Posted December 16, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/185409-pretty-urls-and-forms/ Share on other sites More sharing options...
cags Posted December 16, 2009 Share Posted December 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185409-pretty-urls-and-forms/#findComment-978803 Share on other sites More sharing options...
Daniel0 Posted December 16, 2009 Share Posted December 16, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/185409-pretty-urls-and-forms/#findComment-978810 Share on other sites More sharing options...
lankyzor Posted December 16, 2009 Author Share Posted December 16, 2009 RewriteCond %{QUERY_STRING} q=([^&]+) RewriteRule ^search.php$ /search/%1.html [R] That code creates an infinite loop. This is going to take some tinkering. Quote Link to comment https://forums.phpfreaks.com/topic/185409-pretty-urls-and-forms/#findComment-978816 Share on other sites More sharing options...
Daniel0 Posted December 17, 2009 Share Posted December 17, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/185409-pretty-urls-and-forms/#findComment-978817 Share on other sites More sharing options...
lankyzor Posted December 17, 2009 Author Share Posted December 17, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/185409-pretty-urls-and-forms/#findComment-978827 Share on other sites More sharing options...
lankyzor Posted December 17, 2009 Author Share Posted December 17, 2009 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); } Quote Link to comment https://forums.phpfreaks.com/topic/185409-pretty-urls-and-forms/#findComment-978840 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.