Viewed   104 times

This works:


<a href="/search/querystring">query</a>


RewriteRule ^search/([-0-9a-z]+)$ /search.php?q=$1 [L]

Going through a search form:

<form method="get" action="/search">
<input type="search" name="q" value="querystring" />
<input type="submit" />
  • Result:
  • Desired result:

Is this possible with htaccess or do I need to redirect with PHP from within search.php?

Example desired result in action:


I prefer not to be dependant on JavaScript to do this so search engines and folks with JavaScript disabled will see this too.



I think the problem is that you've created an HTML form with GET method, which automatically opens the URL that way you specified as the result. If you want to submit your search query like the desired one, you should hack the form with some JavaScript to call your good-looking URL like this:

<form method="get" action="/search/" onsubmit="return false;">
<input type="search" name="q" value="querystring" />
<input type="submit" onclick="window.location.href=this.form.action + this.form.q.value;" />
Sunday, September 18, 2022

You have the idea by using the rewrite rule, but you have missed out of the last bit.

RewriteRule ^([a-zA-Z0-9]+)$ index.php?id=$1

The $1 will take the first varable entered so you can have /index/example and the word example can be pulled as a _get['id']

You can create multipule in this way:

RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?id=$1&login=$2

and so on.


If you just want to make the URL look nicer follow this link and read up on all the different ways of using the rewrite rule.

You can change

To look more like this:

If thats what you are looking for then the rule you want to use is:

RewriteEngine On    # Turn on the rewriting engine
RewriteRule    ^pet-care/?$    pet_care_info_01_02_2008.php    [NC,L]    # Handle requests for "pet-care"

Quoted from

The "RewriteRule" line is where the magic happens. The line can be broken down into 5 parts:

RewriteRule - Tells Apache that this like refers to a single RewriteRule.

^/pet-care/?$ - The "pattern". The server will check the URL of every request to the site to see if this pattern matches. If it does, then Apache will swap the URL of the request for the "substitution" section that follows.

pet_care_info_01_02_2003.php - The "substitution". If the pattern above matches the request, Apache uses this URL instead of the requested URL.

[NC,L] - "Flags", that tell Apache how to apply the rule. In this case, we're using two flags. "NC", tells Apache that this rule should be case-insensitive, and "L" tells Apache not to process any more rules if this one is used.

# Handle requests for "pet-care" - Comment explaining what the rule does (optional but recommended)

Wednesday, October 19, 2022

You need to include the QSA flag at the end of your rule. This appends any existing query string to the one that you're constructing in the rule itself:

RewriteEngine On
RewriteRule ^category/([0-9]+)/[a-z][-a-z0-9]*$ cat.php?id=$1 [L,QSA]

So the rewritten URI should end up being: /cat.php?id=23&page=3

Saturday, November 5, 2022

rewrite rules don't change the url, they just tell your server to interpret url that falls under the rule ^search/([a-zA-Z0-9]+)/$ like index.php?search=$1.

If you want to change the link that will be used, you have to change it in your html <form action=''

Friday, December 9, 2022

With generate url use like that (see more

        ['story/create', 'id' =>39,'usr'=>'11'], 
        ['class' => 'profile-link'])

In urlManager input new rule:

rules' => array(
  'story/create/<id:d+>/<usr:d+>' => 'story/create',


Output url will be like that:


And in controller:

public function actionCreate($id, $usr)

And Yii2 provide this parameter.

Monday, December 5, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :