How to Convert a PHP Query String into a Slash-Based Url

How do I convert a PHP query string into a slash-based URL?

Simple .htaccess example:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^lookup/([a-z0-9\-]+)/item/?$ /lookup.php?id=$1
</IfModule>

This will match any alphanumeric (also will recognise dashes) string of any length as the 'id'. You can limit this to just numeric by changing the regex to ([0-9]+).

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^lookup/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ /lookup.php?id=$1&view=$2
</IfModule>

This one will match /lookup/123/some-text/ to /lookup.php?id=123&view=some-text

How to convert query string to url slug on form submit get method in php?

The flow is this.

submit your form to route.php

here is the code to route.php

if(isset($_GET['username']) && isset($_GET['company'])  && isset($_GET['email']) )
$url = '/info/'.$_GET['username'].'/'.$_GET['company'].'/'.$_GET['email']
header('Location: '.$url);

In your .htaccess

RewriteRule  ^info/(.+)/(.+)/(.+)$ info.php?username=$1&company=$2&email=$3 [L,QSA]

Rewrite Rule in htaccess convert query string into slashes php

Have your site root .htaccess as this:

Options -MultiViews
RewriteEngine On
RewriteBase /jobportal/

# To externally redirect /search.php?q=product&l=london to /search/query/london/
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)&l=([^\s&]+)\s [NC]
RewriteRule ^ search/%1/%2/? [R=301,L,NE]

# To externally redirect /search.php?q=product to /search/query/
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)\s [NC]
RewriteRule ^ search/%1/? [R=301,L,NE]

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
RewriteRule ^ %1 [R=301,NE,L]

# ignore all rules below for real files/directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# internal forward from /search/product/ to search.php?q=product
RewriteRule ^search/([^/]+)/?$ search.php?q=$1 [NC,L,QSA]

# internal forward from /search/product/london/ to search.php?q=product&l=london
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?q=$1&l=$2 [NC,L,QSA]

# internal forward from /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Need to pass backslashes in query string with pretty urls

The reason you get a 404 error is because by default, Apache has AllowEncodedSlashes turned off, and its behavior is to 404 any request containing a backslash. This is because having it turned on is a potential security hazard. In Apache 2.2.18 and later, there is a NoDecode setting which makes this a bit safer, and works well for your case.

Anyway I tried turning it on locally, and indeed this script:

<?php var_dump($_REQUEST); ?>

is able to handle urls like

http://localhost/this\is\a\test

and outputs

array(1) { ["arguments"]=> string(14) "this\is\a\test" } 

However, it (or my browser) seems to have problems with encoded backslashes, converting them to regular backslashes - maybe that's not a problem. If you enable the B flag on your rewriterule, it will convert unencoded backslashes to encoded backslashes.

slashes in url variables

You need to escape the slashes as %2F.

How to rewrite query string to slashes in .htaccess

This is probably what you are looking for:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(\d+)$
RewriteRule ^/?profile\.php$ /profile/%1 [R=301,QSD]

This will externally redirect an incoming request to the resource /profile.php?id=2 to the URL /profile/2, so change the URL in the browser.


In case you receive a http status 500 (internal server error) for this chances are that your http server is a very old version and you have to use a workaround to get a more or less clean result:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(\d+)$
RewriteRule ^/?profile\.php$ /profile/%1? [R=301]

The above rule set should work likewise in the http servers (virtual) host configuration or in dynamic configuration files (".htaccess" style files). A dynamic configuration file would have to be placed inside the http server hosts DOCUMENT_ROOT location.


And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

htaccess file to use forward slash instead of ?, = and & signs in query string?

Try with below, I am using mod_rewrite to achieve the result, clear cache if using any previous attempts.

Below will also cover '_' underscore and next rule after will be for only one item.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z\_]+)/([0-9]+)/([a-zA-Z]+)/([a-zA-Z]+)$ $1.php?id=$2&some1=$3&some2=$4 [L]
RewriteRule ^([a-zA-Z\_]+)/([0-9]+)$ $1.php?item=$2[L]


Related Topics



Leave a reply



Submit