1

TL;DR

turn search.php?key=value => value.php

I have a simple project:

|-project
|-----.htaccess
|-----index.php
|-----jquery.min.js
|-----search.php

All I'm trying to learn is how to turn query params into page.php, e.g.:

?search=test becomes test.php

I found this SO post: htaccess rewrite for query string

Which suggests 3 methods of doing it, I've tried all yet my search.php doesn't work.

Here is my index.php

<html>
<body>
    <form method="post">
        <input type="text" name="search" placeholder="search something" />
    </form>

    <button type="button" id="my-btn">Submit</button>

    <script src="jquery.min.js"></script>

    <script>
        jQuery(document).ready(function($)
        {
            $('#my-btn').click(function()
            {
                let val = $('input[type="text"]').val();


                $('form').attr('action', 'search.php?term='+ val);
                $('form').submit()
            })
        })
    </script>
</body>
</html>

which goes to search.php

<?php
    $search = $_GET['search'];

    echo '<pre>';
    echo 'Search Term: <strong>'. $search .'</strong>';
    echo '</pre>';

    echo '<hr />';

and my .htaccess file looks like this:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ /search.php?term=$1 [L]

But this (or the other methods) didn't work. My url still is search.php?term=test - how do I go about achieving my goal?

22
  • You may want to look at documentation and check what the arguments for the RewriteRule directive do. Commented Oct 2, 2018 at 15:47
  • @miken32 I looked but honestly not sure it stuck, apache stuff doesn't seem to stick for me .. but I'll keep re-reading until some light turns on Commented Oct 2, 2018 at 15:59
  • You are way over complicating this simply do this <form method="get" action="text.php"> and get rid of the JS and rewrite stuff. There is nothing wrong with using a get form for a search, in fact that's the most common use of it. That is what it is meant for, so the url query can be bookmarked. Commented Oct 2, 2018 at 16:00
  • 1
    Learn Regex (Regular Expressions) they are a bit archaic and confusing, but once you understand them they are extremely powerful. If you put mysite.com/test.php it will map it to mysite.com/search.php?page=test For example regex101.com/r/Ax5qBN/1 Commented Oct 2, 2018 at 16:13
  • 1
    @ArtisticPhoenix I believe that is something I definitely need to improve on and develop skill-wise. Tis probably one of the major things holding back my development Commented Oct 2, 2018 at 16:14

2 Answers 2

1

You may use this code in project/.htaccess:

RewriteEngine On

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+search(?:\.php)?\?term=([^\s&]+) [NC]
RewriteRule ^ /%1.php? [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.php$ search.php?term=$1 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ search.php?term=$1 [L,QSA]
Sign up to request clarification or add additional context in comments.

Comments

1

How about

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} term=(.*)
RewriteRule ^.*$ %1.php [L]

10 Comments

that produces: 403 forbidden with message: You don't have permission to access /.php on this server. but I think right path maybe?
close .. produced a 404 :S
I've tested this, and it's doing what you expect for me. If you get a 404, is it because the php file doesn't exist?
it doesn't - basically, search.php is the file, but in url should look like queryValue.php, e.g. search.php?key=value becomes value.php (with search.php behind it)
"thats what I'm trying to do (value + .php) however - that file doesn't physically exist" :) Seems you are trying to show a different file name on the browser bar but the real file that is executed is another... may by include the file to hide and just execute the fake one
|

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.