Php search form and url rewrite / formatting

So, I am working on my own project, and this is related to job search. I want the URLs to display as: site.com/search/developer, where the developer search keyword is obvious. Now the thing is as usual, when I click search (the send button does this work), and a URL appears in the form: site.com/search.php?kword=developer and Im sure htaccess rewrite cannot change the way php forms behave and how they format the url.

Now I have an idea to use javascript so that the user enters a keyword, I take this and redirect pages through js to site.com/search/[keyword], but I will MAKE SURE if this is a good idea (given that javascript is disabled).

Is there a decent / recommended way to achieve I'm trying to achieve, I know that there is, because I saw some great websites, it’s only I don’t know what is the best way to do this.

Thanks in advance.

+4
source share
1 answer

You can use Javascript to make it site.com/search/developer . For the extremely rare case when Javascript is disabled, you can let them submit a standard HTML form with a final URL like site.com/search.php?kword=developer .

The good news is that with mod_rewrite you can handle both cases to always have a pretty URL in the browser.

Include mod_rewrite and .htaccess through httpd.conf , and then put this code in .htaccess in the DOCUMENT_ROOT directory:

 Options +FollowSymLinks -MultiViews # Turn mod_rewrite on RewriteEngine On RewriteBase / # internal forward RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^search/(.+?)/?$ /search.php?kword=$1 [L,QSA,NC] # external rewrite RewriteCond %{THE_REQUEST} ^[AZ]{3,}\s/+search\.php\?kword=([^\s]+) [NC] RewriteRule ^ /search/%1? [R=302,L] 

Once you confirm that it is working properly, replace R=302 with R=301 . Avoid using R=301 (Permanent Forwarding) when checking mod_rewrite rules.

+3
source

Source: https://habr.com/ru/post/1487560/


All Articles