htaccess files: Changing URL to username - apache

In the following thread:
How to create user profiles with PHP and MySQL
I have a number of doubts:
In the first answer (by Chacha102) what is the '$1', I understand that it refers to a paramter but which one?
Does not the code in the first answer redirect to another index.php without renaming the url to something like www.facebook.com/username?
UPDATE:
Where does the edit to the url occur?

Doubt 1:
It refers to value of a get parameter user. For instance, if you have index.php?user=Name it refers to "Name".
Doubt 2:
The code wouldn't redirect. It will just rewrite url to www.domain.com/Name. It's equivalent to www.domain.com/index.php?user=Name.

1: The $1 is called the first captured group from the pattern. It refers to the value ?user=john (Read more about capturing groups)
2: In most of the PHP application the main entry (routing) point of http request is index.php. If you enter the url like http://www.example.com/john actually it would be the same as http://www.example.com/index.php?user=john if you have applied the same mod_rewrite rule from the answer.

Related

How do I enable query string in Cloudflare page rules?

I want to forward this URL
https://demo.example.com/page1?v=105
to
https://www.example.com/page1
It's not working if I set so:
But if I remove the ?v= part, it works:
Is there any way to include the ?v= part in this page rule?
The rule you mentioned works only for that exact URL: https://demo.example.com/page1?v=105. If it has any additional query parameter or even an additional character, eg https://demo.example.com/page1?v=1050, it won't match the rule.
If you need to use complicated URL matches that require specific query strings, you might need to use bulk redirects or a Cloudflare worker.

Yii transform url from param/param/param/ to param/param?param

Suppose I have a url like:
site.com/param1/value1/param2/value2/param3/value3/param4/value4
I need to convert this url when a user writes it in url line to:
site.com/param1/value1/param2/value2?param3=value3&param4=value4
P.S. - the number of parameters is variable.
How can I do it?
You need to change the UrlManager Rules according to the situation.
You need to configure the Url manager to handle the first two params as path url and let the rest

mod_rewrite to change query string parameter name

I need help writing a mod rewrite rule to change the name of a query string parameter. I want to change the name, not the value.
old name partner
new name a_aid
so a link like this
http://domain.com/?partner=derphipster&pname=foo&plink=http%3A%2F%2Fbar.com%2Ffoo
will become
http://domain.com/?a_aid=derphipster&pname=foo&plink=http%3A%2F%2Fbar.com%2Ffoo
I found this article but the accepted answer generated errors for the OP:
mod_rewrite - old parameter name to new name
also this article, but the solution was to use PHP. which will not work in my case:
APACHE mod_rewrite change variable name in query string
I can't use PHP because some affiliate tracking code creates a cookie from the query string--and expects the a_aid. So I'm trying to convert partner into a_aid for it
OK think I hacked it together on my own. Please post an answer if you think its brittle or could be done better and I'll accept yours instead
RewriteCond %{QUERY_STRING} ^(.*)partner(.*)$
RewriteRule ^(.*)$ $1?%1a_aid%2 [R=301,L]

Pretty URL & SEO-friendly URL?

I'm currently using Apache 2.2
I can do simple things like
RewriteRule ^/news$ /page/news.php [L]
RewriteRule ^/news/(.*)$ /page/news.php?id=$1 [L]
but what if I want to send 2 parameters like this
http://www.example.com/link/param1/param1_value/param2/param2_value
Lastly, I want to also know implementing SEO friendly URL like stackoverflow
though I can get access to a page with URL like
http://www.example.com/doc_no/
Just decorating that URL with
http://www.example.com/doc_no/this-is-the-article
Give me some suggestion with example snippets.
I know that the PHP symfony framework allows you to do that.
How does it work :
In apache config, use mod_rewrite to redirect ALL incoming resquest to a single entry point (in symfony this is called the "front controller")
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
In this front controller you are going to create a "Request" object which holds all the informations provided by the URL.
For example you could say that the first thing after the "/" is the name of the PHP file to call and everything else are parameters and values so that :
http://example.com/file/id/2 will call file.php with id=2
To do that, just use some reg exp and design you "Request" class carefully. For the example above the "Request" class should provide both getRequestedAction() and getParameter(string parameter) methods. The getRequestedAction() method will be used when the "Request" object is fully populated in order to call the correct file/action/method.
if you choose to populate the parameter array of the request object with both reg exp on the URL and a parsing of the _GET array, you may get to the point where :
http://example.com/file/id/2 is the same as http://example.com/file?id=2
(and both can work)
you can choose to ignore extensions (http://example.com/file.html is the same as http://example.com/file), or not.
Finally, for some URL, you can choose to just ignore everything that goes after the last '/'. So that : http://example.com/question/3/where-is-khadafi is the same as http://example.com/question/3/is-linux-better-than-windows
In the different file.php, just use $request->getParameter('id') to get the value of the "id" parameter, instead of using the _GET or _POST arrays.
The whole point is to
Redirect all incoming traffic to a single "front controller"
In that file, create a "Request" object that contains all the informations needed to run the site
Call the correct action (php file) based on the informations contained in that "Request" object
Inside the actions, use this request object to fetch the parameters contained in the URL
Hope this helps
Note Google have stated that they prefer news.php?id=$1 instead of news/$1 because it is easier for them to detect the variable. This is more pertinent when increasing the number of variables as just looking at your first example is a bit confusing:
http://www.example.com/link/param1/param1_value/param2/param2_value
You can always combine the two if one parameter is generic like a category:
http://www.example.com/param1/?id=param2_value
One should really reevaluate the design if more than one parameter is required and it is not a temporary search.

Controller not found issue when rewriting url with exclamation mark

In monorail I'm trying to create a url rewriting rule to give friendly urls to article posts. Here's what the urls look like:
http://domain.com/2010/11/29/Winter-snow-warning
And here's the code in global.asax.cs to rewrite the urls:
RoutingModuleEx.Engine.Add(
new PatternRoute("/<year>/<month>/<day>/<title>")
.DefaultForController().Is("post")
.DefaultForAction().Is("show")
.Restrict("year").ValidInteger
.Restrict("month").ValidInteger
.Restrict("day").ValidInteger
);
This works great, however if there is an exclamation point in the url:
http://domain.com/2010/11/29/Winter-snow-warning!!
Then it doesn't match the rewriting rule and errors out, saying the controller "2010" cannot be found. What am I missing here, is this a bug in monorail?
Perhaps the default matching mechanism of Monorail's routing is not accepting exclamation mark, thus the route does not match and the default /controller/action rule is matched instead, failing since no 2010 controller exists.
A quick workaround could be to to restrict the title to the exact expression that fits your needs. e.g.: .Restring("title").ValidRegex("[-_.+!*'() a-zA-Z0-9]+]")