Mobile app version of vmapp.org
Login or Join
Speyer207

: Rewrite URL with multiple queries This article describes how to direct incoming traffic coming from "fake" URLs of this type: http://www.example.com/seo-sample/article-install-apache-on-linux.php to

@Speyer207

Posted in: #Htaccess #ModRewrite #UrlRewriting

This article describes how to direct incoming traffic coming from "fake" URLs of this type:
www.example.com/seo-sample/article-install-apache-on-linux.php

to a PHP file that "really" uses URLs of this type:
www.example.com/seo-sample/article_show_friendly.php?url=install-apache-on-linux

Here is the example code used in the htaccess file:

RewriteEngine on
RewriteRule ^article-(.*).php$ ./article_show_friendly.php?url=


However, my pages have multiple queries in their URLs. How do I make this work for them?
example.com/keyboard/keyboard-chart.php?gam=141&sty=16&lay=1&tit=sid-meiers-alpha-centauri-smac-1

(I want to use the tit parameter as the main part of the URL.)

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Speyer207

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

example.com/keyboard/keyboard-chart.php?gam=141&sty=16&lay=1&tit=sid-meiers-alpha-centauri-smac-1

I want to use the "tit" parameter as the main part of the URL.


If you literally only want to use that one parameter, and you don't need to pass the other parameters in the URL (ie. they can be deduced from that one parameter), then it's exactly the same as your first example. You would have a "pretty" (fake) URL of the form:
example.com/keyboard/sid-meiers-alpha-centauri-smac-1

(No need for the .php extension on the URL, unless you explicitly want it?) Which is then internally rewritten with something like the following in the /keyboard/.htaccess file:

RewriteRule ^([w-]+)$ keyboard-chart.php?gam=141&sty=16&lay=1&tit= [L]


In the above example, the gam, sty and lay parameters are hard-coded in the RewriteRule substitution. This assumes that given a tit parameter of sid-meiers-alpha-centauri-smac-1, you know what these other parameter values are.

However, if that is not the case and these other parameters can also vary, then these must also be passed in the "pretty" URL, since they cannot be deduced from the single tit parameter. This can be achieved using the same principle as above: define a capturing group (ie. parenthesised sub pattern) in the RewriteRule pattern (regex) for each parameter and use a corresponding backreference (..) in the substitution. For example, given a URL of the form:
example.com/keyboard/141/16/1/sid-meiers-alpha-centauri-smac-1

Then this can be internally rewritten with something like the following in the /keyboard/.htaccess file:

RewriteRule ^(d+)/(d+)/(d)/([w-]+)$ keyboard-chart.php?gam=&sty=&lay=&tit= [L]



coming from "fake" URLs


I know what you mean, but they aren't really fake. They are the "pretty" URLs, or simply "URLs". It's just that they don't directly map to a file system resource, without some URL rewriting. (By "fake" I immediately thought you were referring to URLs that didn't exist!)


SEO Friendly URLs


The article that you link to makes repeated reference to "SEO friendly URLs". To be clear, these are not strictly SEO-friendly. Search engines don't really care. "Pretty" URLs like this are primarily for the benefit of users, not search engines, and can help with click-through rates, but they don't directly help with ranking.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme