4

So I'm working on a self project of mine and it is related to job searching. I want the urls to appear as: site.com/search/developer where developer is the search keyword obviously. Now the thing is that as normally when I click search (the submit button does it's job) and the url appears as: 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 when user types a keyword, i take that and redirect the page via js to site.com/search/[keyword] but I'm UNSURE if this is a good idea (considering disabled javascript cases).

Is there any decent/recommended way to achieve what I'm trying to achieve, I know there is because I have seen some big websites do it only I dont know what's the best way to do it.

Thank you in advance.

1 Answer 1

4

You can certainly use Javascript to make it site.com/search/developer. For the extreme rare case when Javascript is disabled you can let them submit regular HTML form with the final URL as site.com/search.php?kword=developer.

Good news is that using mod_rewrite you can handle both the cases to always have pretty URL in the browser.

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under 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} ^[A-Z]{3,}\s/+search\.php\?kword=([^\s]+) [NC]
RewriteRule ^ /search/%1? [R=302,L]

Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Sign up to request clarification or add additional context in comments.

1 Comment

Hello Thanks for your reply. I neeed to confirm what was the best approach to solving this situation. And hey thanks for the htaccess code too and for the recommendations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.