0

Here is the html form with select tag with option value :

<select class="form-control" id="basic" name="location" required>
<?php
$get_location = mysqli_query($conn, "SELECT * FROM product_sub_area");
if(mysqli_num_rows($get_location) == 0 ) {
    $choose = 'No City found';
} else {
    $choose = 'Choose City';
}
?>
<option value=""><?php echo $choose; ?></option>
<?php                                         
while($get_location_result = mysqli_fetch_array($get_location) ) {
    $location_id = (int) $get_location_result['psub_area_id'];
    $location_name = htmlspecialchars($get_location_result['psub_name']);
    echo "<option value='$location_name'>$location_name</option>";
}
?>                    
</select>

After submit this form, page url is showing like this :

http://localhost/freelancer/sitename/location?location=California

I want the url should look like this for every same type of url request. It could be multiple query string :

http://localhost/freelancer/sitename/location/California

I am using following .httaccess rules :

ErrorDocument 404 /not-found.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ location.php?location=$1 [NC,L]  
1

2 Answers 2

1

Try this in sitename folder, what i observe from your requirement you are removing php extension and rewriting request to location parameter.

ErrorDocument 404 not-found.php
RewriteEngine On        

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

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Sign up to request clarification or add additional context in comments.

4 Comments

let me try with this.
I am trying with this url : localhost/freelancer/sitename/location/hello but ti's showing me Internal Server Error
Note : my .htaccess file is now in my main root folder called : sitename
I am assuming you are trying this in sitename folder and also please check error document is also intact every filename path given correctly and then try i've edited my code too.
0

In this case you have to use a JavaScript to trigger on the submit, which then alters the URL submitted to include just the state name. You can either use jQuery for this, or standard JS. The two solutions are pretty similar, so you can rework the jQuery to vanilla pretty easily.

Reason why you have to use JS is because this all happens before you're sending the request to the server, and it is this very request that you want to change.

PS: Remember to add fallbacks for non-JS enabled browsers (yes, they do exist) and web-crawlers.

Comments

Your Answer

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