Rectangle 27 2

RewriteEngine on
RewriteBase /

# Skip actual files/directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# If the host matches example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com$
# And the request URI starts with page.php
RewriteCond %{REQUEST_URI} ^\/page\.php
# And the querystring matches tokenid=DIGIT&tokenname=TOKEN_NAME
RewriteCond %{QUERY_STRING} tokenid=\d&tokenname=(.*)
# Then rewrite the URI to TOKEN_NAME
RewriteRule ^(.*)$ %1? [L,R]

You might want to try it online, if you wish.

i have tried your suggestion, but i am getting error 404 page, i am not able to retrive the page content.

As far as the apache's rewrite module is concerned, you're correctly redirecting to /About Us page. You need to make sure that the URI actually exists. Adding a rewrite rule does not magically make that page available.

yes but i am fetching the page from database based on tokenid and tokenname and i am rewriting the url so that the user does not see the extra query string .

Url rewriting in php with multiple query string variables using .htacc...

.htaccess url-rewriting query-string
Rectangle 27 243

Submitting multi-value form fields, i.e. submitting arrays through GET/POST vars, can be done several different ways, as a standard is not necessarily spelled out.

Three possible ways to send multi-value fields or arrays would be:

?cars[]=Saab&cars[]=Audi
?cars=Saab&cars=Audi
?cars=Saab,Audi

On a form, multi-valued fields could take the form of a select box set to multiple:

<form> 
    <select multiple="multiple" name="cars[]"> 
        <option>Volvo</option> 
        <option>Saab</option> 
        <option>Mercedes</option> 
    </select>
</form>
some_name[]

multiple hidden fields with the same name

<input type="hidden" name="cars[]" value="Volvo">
<input type="hidden" name="cars[]" value="Saab">
<input type="hidden" name="cars[]" value="Mercedes">

NOTE: Using field[] for multiple values is really poorly documented. I don't see any mention of it in the section on multi-valued keys in Query string - Wikipedia, or in the W3C docs dealing with multi-select inputs.

As commenters have pointed out, this is very much framework-specific. Some examples:

?list_a=1&list_a=2&list_a=3&list_b[]=1&list_b[]=2&list_b[]=3&list_c=1,2,3
"list_a": "3", 
"list_b":[
    "1",
    "2",
    "3"
  ], 
"list_c": "1,2,3"
"list_a": [
    "1",
    "2",
    "3"
  ],
  "list_b[]": [
    "1",
    "2",
    "3"
  ],
  "list_c": "1,2,3"

Maintaining order: One more thing to consider is that if you need to maintain the order of your items (i.e. array as an ordered list), you really only have one option, which is passing a delimited list of values, and explicitly converting it to an array yourself.

I think the reason that this functionality is not documented in general sources like Wiki or W3C is that it is not generally supported by web frameworks. PHP and I think Rails automatically convert multiple "key[]" query parameters into an array. Others don't.

The nodejs querystring module uses the ?cars=Saab&cars=Audi form

In express (node.js), both ?cars=Saab&cars=Audi and ?cars[]=Saab&cars[]=Audi get turned into arrays. However, ?cars=Saab ends up as a string, but ?cars[]=Saab is an array with a single element.

Can you set keys using this approach? And what about multi-dimensional arrays?

ASP.NET MVC doesn't appear to read the format ?cars[]=Saab&cars[]=Audi There have either be no square brakets or the square brackets need indexes/

php - How to pass an array within a query string? - Stack Overflow

php arrays query-string querystringparameter
Rectangle 27 243

Submitting multi-value form fields, i.e. submitting arrays through GET/POST vars, can be done several different ways, as a standard is not necessarily spelled out.

Three possible ways to send multi-value fields or arrays would be:

?cars[]=Saab&cars[]=Audi
?cars=Saab&cars=Audi
?cars=Saab,Audi

On a form, multi-valued fields could take the form of a select box set to multiple:

<form> 
    <select multiple="multiple" name="cars[]"> 
        <option>Volvo</option> 
        <option>Saab</option> 
        <option>Mercedes</option> 
    </select>
</form>
some_name[]

multiple hidden fields with the same name

<input type="hidden" name="cars[]" value="Volvo">
<input type="hidden" name="cars[]" value="Saab">
<input type="hidden" name="cars[]" value="Mercedes">

NOTE: Using field[] for multiple values is really poorly documented. I don't see any mention of it in the section on multi-valued keys in Query string - Wikipedia, or in the W3C docs dealing with multi-select inputs.

As commenters have pointed out, this is very much framework-specific. Some examples:

?list_a=1&list_a=2&list_a=3&list_b[]=1&list_b[]=2&list_b[]=3&list_c=1,2,3
"list_a": "3", 
"list_b":[
    "1",
    "2",
    "3"
  ], 
"list_c": "1,2,3"
"list_a": [
    "1",
    "2",
    "3"
  ],
  "list_b[]": [
    "1",
    "2",
    "3"
  ],
  "list_c": "1,2,3"

Maintaining order: One more thing to consider is that if you need to maintain the order of your items (i.e. array as an ordered list), you really only have one option, which is passing a delimited list of values, and explicitly converting it to an array yourself.

I think the reason that this functionality is not documented in general sources like Wiki or W3C is that it is not generally supported by web frameworks. PHP and I think Rails automatically convert multiple "key[]" query parameters into an array. Others don't.

The nodejs querystring module uses the ?cars=Saab&cars=Audi form

In express (node.js), both ?cars=Saab&cars=Audi and ?cars[]=Saab&cars[]=Audi get turned into arrays. However, ?cars=Saab ends up as a string, but ?cars[]=Saab is an array with a single element.

Can you set keys using this approach? And what about multi-dimensional arrays?

ASP.NET MVC doesn't appear to read the format ?cars[]=Saab&cars[]=Audi There have either be no square brakets or the square brackets need indexes/

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

php - How to pass an array within a query string? - Stack Overflow

php arrays query-string querystringparameter
Rectangle 27 1

Assuming your .htaccess is in the web root / directory

RewriteEngine on
RewriteBase /

RewriteCond %{QUERY_STRING} !no-redir [NC]
RewriteCond %{QUERY_STRING} (^|&)subject=([^&]+) [NC]
RewriteRule ^(.*)/product.php$ $1/%2? [NC]

RewriteRule ^([^+\s]+)(?:[+\s]+)([^+\s]+)((?:[+\s]+).*)$ $1-$2$3 [DPI,N]
RewriteRule ^([^+\s]+)(?:[+\s]+)([^+\s]+)$ $1-$2 [R=301,DPI,L]

Chris, the idea is to now capture the subject from path info instead of a GET parameter because it's not being passed as ?subject=sub+info which makes it unavailable as $_GET['subject'] now.

You'd need to get the subject keywords from the URI path as follows:

$uri = $_SERVER['REQUEST_URI'];
$subject = substr($uri, strrpos($, "/"));

Add the following after the rules defined above.

RewriteRule ^(.*/popcorn)/([^/.]+)$ $1/product.php?subject=$2&no-redir [NC,L]

Notice, I've added another RewriteCond on %{QUERY_STRING} above. This also means you can revert your product.php to the way it was before since subject is being passed again as usual.

That code worked perfectly to reformat the URL's, however I am now getting a 404 Server Error. The landing page has a conditional to check if the $subject variable isset before the page loads - would the reformatted URL (without the 'subject=') affect the conditional?

Yes, it would. The landing page won't recognize subject unless passed as ?subject=. I think formatting the url as /shop/popcorn/?subject=Bacon-and-Cheddar-Popcorn should work. Your call. Shorten it to just s if you like or pass as /?Bacon-and... Your php would have to accommodate accordingly. I would suggest you keep the pretty url and use lastIndexOf "/" with sub-string to capture the subject.

Thanks for the help Ravi, is 'lastIndexOf"/" with sub-string' a JavaScript function? I am not familiar with a PHP function of that name. If you could provide any direction or links on how to use that technique I would be very appreciative.

@Chris, those were Java equivalents of PHP strrpos() and substr(). Please, have a look at my update. That's the change I'm proposing for your landing php page.

Ravi, I was able to utilize the $_SERVER['REQUEST_URI'] variable to satisfy the conditional

php - Formatting .htaccess RewriteCond / RewriteRule for removing quer...

php apache .htaccess rewrite
Rectangle 27 235

A line break is defined differently on different platforms, \r\n, \r or \n.

Using RegExp to split the string you can match all three with \R

$array = preg_split ('/$\R?^/m', $string);

That would match line breaks on Windows, Mac and Linux!

I used this as well instead of the accepted answer and any other answers on this thread. Just leaving this comment as an informative.

Actually, this did not work for me. Sometimes newlines where still present in the array keys.

$array = preg_split ('/\R/', $string);
preg_split("/\r\n|\n|\r/", $string)

php - How to put string in array, split by new line? - Stack Overflow

php string line-breaks
Rectangle 27 235

A line break is defined differently on different platforms, \r\n, \r or \n.

Using RegExp to split the string you can match all three with \R

$array = preg_split ('/$\R?^/m', $string);

That would match line breaks on Windows, Mac and Linux!

I used this as well instead of the accepted answer and any other answers on this thread. Just leaving this comment as an informative.

Actually, this did not work for me. Sometimes newlines where still present in the array keys.

$array = preg_split ('/\R/', $string);
preg_split("/\r\n|\n|\r/", $string)

php - How to put string in array, split by new line? - Stack Overflow

php string line-breaks
Rectangle 27 1

RewriteCond %{QUERY_STRING} ^companynumber=([0-9]*)$
RewriteRule ^company\.php$ http://test.domain.com/company/%1/? [R,L]

Then remap the new incoming URL via:

RewriteRule ^company/(\d+)/?$ company.php?companynumber=$1 [END]

Note that this:

  • Should carry the [END] flag to avoid another internal rewrite round.
\d+

See also "Ping-Pong rewrites" on Reference: mod_rewrite, URL rewriting and pretty links explained. These rewrites should just be used as temporary measure. Establish the new URLs right in your HTML templates.

Thank you so much @Mario! I will try and use the above now and report back. Much appreciated.

That worked. Thank you so much! You have done in 2 minutes what would have taken me weeks. Thanks again!

php - .htaccess Query String Rewrite - Stack Overflow

php apache .htaccess mod-rewrite url-rewriting
Rectangle 27 1

It should work if you skip the ^ and the $ like this:

RewriteCond %{QUERY_STRING} link=androidapp 
RewriteRule ^(.*)$ http://www.example.com/test.php? [R=301,L]

The ^ means that the your pattern matches only from the beginning of the querystring, but your querystring begins with id=15...

php - htaccess redirect with query string - Stack Overflow

php apache .htaccess mod-rewrite redirect
Rectangle 27 2

I resolved the issue by changing internal links for all pages instead. Thank you for everyone's help. If you have a solution that actually fixes it via the htaccess file I would be extremely interested. Thanks

php - .htaccess hiding string from URL 301 Redirects (including specia...

php apache .htaccess mod-rewrite redirect
Rectangle 27 2

You want to use .htaccess to rewrite your URLs internally and route them appropriately to your application.

Here's a specific .htaccess example that will do what you're looking for:

RewriteEngine on
RewriteRule ^cat_([0-9]*) /?cat=$1 [N,QSA]
RewriteRule ^item_([0-9]*) /?item=$1 [N,QSA]

Just put the above in a file called .htaccess in your root web folder, and you'll be good to go.

Note that you may have to enable mod-rewrite in your apache server if it's not already enabled (most shared hosting providers have it enabled)

php - User-friendly URLs instead of Query Strings? - Stack Overflow

php url
Rectangle 27 2

You want to use .htaccess to rewrite your URLs internally and route them appropriately to your application.

Here's a specific .htaccess example that will do what you're looking for:

RewriteEngine on
RewriteRule ^cat_([0-9]*) /?cat=$1 [N,QSA]
RewriteRule ^item_([0-9]*) /?item=$1 [N,QSA]

Just put the above in a file called .htaccess in your root web folder, and you'll be good to go.

Note that you may have to enable mod-rewrite in your apache server if it's not already enabled (most shared hosting providers have it enabled)

php - User-friendly URLs instead of Query Strings? - Stack Overflow

php url
Rectangle 27 1

RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f  
RewriteRule ^registration/(.*)$ ./registration.php?id=$1

php - htaccess remove only '?=' sign from URL with query string - Stac...

php .htaccess url url-rewriting
Rectangle 27 1

RewriteEngine on
RewriteRule registration/id/(\d+)$ /16-7-2013/PHP_NEW/registration.php?id=$1 [L]

php - htaccess remove only '?=' sign from URL with query string - Stac...

php .htaccess url url-rewriting
Rectangle 27 1

Both the URIs that you want to redirect look similar except that 2nd one has hyphens - in first query parameter.

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^r/([^-]+)/(.+)/?$ r.php?deb=$1&id=$2 [L,QSA]

RewriteRule ^r/([a-z0-9-]+)/(.+)/?$ r.php?abc_id=$1&id=$2 [L,QSA,NC]

Thanks for the help with little changes I have made it work, But now the problem is its working on my localhost but when I transferred it on the server were I am running virtualhost Its not working can you help ?

On the server make sure .htaccess is enabled and then if .htaccess is correctly paced in DOCUMENT_ROOT directory. If everything is right then add R flag in above 2 RewriteRule lines to see what is the final URI on server.

Thanks a lot :) Is there some how I can keep contact with you any social site or anything ?

php - .htaccess redirect normal url to query string - Stack Overflow

php .htaccess url url-rewriting rewrite
Rectangle 27 1

RewriteRule (.*)/(.*)/(.*)$ path_to_php_files/$1.php?deb=$2&id=$3 [QSA,L,NC]

Unfortunately with this rule, you would have the first $_GET parameter always deb and I hope you have the DirectoryIndex set up corectly. I do not think that for

www.example.com/r/abc/1234
www.example.com/r/12bcd-qsqs-343-wdwd/1234

you can delivrer the abc and 12bcd-qsqs-343-wdwd to 2 different $_GET parameters if you have dynamic links.

Yes, thank you, I removed it, but it should work.

Actually if you notice OP wants to redirect in 2 different ways using 2 different query parameters.

php - .htaccess redirect normal url to query string - Stack Overflow

php .htaccess url url-rewriting rewrite
Rectangle 27 2

This is accomplished through the use of .htaccess to modify how the URL is sent to the client. Or as other people say, to prettify the URL. Here are some links to articles to help you get started with the mod_rewrite rules required to make this function.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

php - User-friendly URLs instead of Query Strings? - Stack Overflow

php url
Rectangle 27 2

This is accomplished through the use of .htaccess to modify how the URL is sent to the client. Or as other people say, to prettify the URL. Here are some links to articles to help you get started with the mod_rewrite rules required to make this function.

php - User-friendly URLs instead of Query Strings? - Stack Overflow

php url
Rectangle 27 1

There should be an index file under the folder "name"

If so, you can try this:

RewriteEngine On
RewriteBase /
RewriteRule ^/$ /collection/name/index.php
RewriteRule ^([a-zA-Z0-9_-]+)/$ /collection/?collection_name=$1

This is a generic rule that works in many similar occasions. Modify it to suit your website...

In that case, you could change the third line to: "RewriteRule ^/$ /collection/index.php"... That is of course if there is an index.php file in the folder "collection"...

It's WordPress so "/collection" is actually a page, not a folder. If I create a "collection" folder it breaks. I think I would need the rule to be in the .htaccess of the root.

@Jamie I think so too, you should include these rules in your root .htaccess file. In that case you should fo course omit the first two lines (since they already exist there). But, I must say that I have minimal to none Wordpress experience, since I usually code my projects from scratch. I did a google search and located 2 interesting articles: wordpress.org/support/topic/htaccess-rewriterule-2 and stackoverflow.com/questions/16664579/ ...You may want to check them out or google "wordpress .htaccess rules" for more results...

php - .htaccess rule to rewrite query string to a path - Stack Overflo...

php wordpress apache .htaccess mod-rewrite
Rectangle 27 1

Looks like you've hit the most common problem people face when switching to pretty URL schemes. Solution is also simple, just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.

You can try adding this in your page's HTML header: <base href="/" /> so that every relative URL is resolved from that URL and not the current URL.

You saved the day. I added the <base href="/" />. That solved it.

I don't know if I should create a separate question or you can answer here. How do I preserve the the "profile" directory like on the url. Like: www.site.com/profile/username/engineer/48?

RewriteRule ^profile/([^/]+)/([^/]+)/([^/]+)$ profile.php?user=$1&title=$2&uid=$3 [L,QSA]

ok that does it internally, also added RewriteRule ^ /profile/%1/%2/%3? [L,R=301]

php - .htaccess redirect url with three query strings - Stack Overflow

php apache .htaccess redirect query-string
Rectangle 27 2

RewriteEngine On
RewriteRule ^staff-view/([^/]*)$ /staff-view.php?i=$1 [L]

Thanks for the answer, I have tried it, but it is still not returning the query string for ACCOUNT_ID_EXAMPLE

apache - .htaccess 301 and PHP query string - Stack Overflow

php apache .htaccess mod-rewrite query-string