Viewed   104 times

This works:

HTML

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

htaccess

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" />
</form>
  • Result: http://example.com/search?q=querystring
  • Desired result: http://example.com/search/querystring

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

Example desired result in action: http://twitter.com/search/hello

EDIT

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

 Answers

4

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;" />
</form>
Sunday, September 18, 2022
4

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.

Update:

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 http://www.pets.com/pet_care_info_07_07_2008.php

To look more like this: http://www.pets.com/pet-care/

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 https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

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
5

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
5

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
 
barak
 
1

With generate url use like that (see more http://www.yiiframework.com/doc-2.0/guide-helper-url.html):

Html::a('<b>Register</b>', 
        ['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:

story/create/39/11

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 :