Simple pretty urls with .htaccess - php

I have a one page blog php website.
Content is dynamicaly loaded based on get parameters. I would like to use my htaccess to make pretty urls. I have these urls:
website.com/index.php?category=review&page=1
And I would like to have this:
website.com/category/review/page/1
And I also use article as get parameter. So I would like to change this:
website.com/index.php?article=12345-name-of-article
To this:
website.com/article/12345-name-of-article
I am totally new to htaccess, so any help would be appreciated.
I tried this rewrite rule:
RewriteRule ^article/([0-9a-zA-Z_-]+)$ index.php?article=$i [NC,L].
It worked somehow, but php script does not recognize url parameters. So it does not work.
Thank you very much!

You need to use QSA - When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined. :
RewriteEngine On
RewriteRule ^article/([\w-]+)(?:\.html|/)?$ index.php?article=$1 [NC,QSA,L]
RewriteRule ^category/([\w-]+)/page/([\w-]+)(?:\.html|/)?$ index.php?category=$1&page=$2 [NC,QSA,L]

Related

Passing variables to a well format URL

My .htaccess is something like this
RewriteRule ^search/([A-Za-z0-9-]+)$ search.php?keyword=$1 [L]
and it correctly displays results for example.org/search/tomato
but now i'd like to pass variables to it, but when i do it like
example.org/search/tomato?color=green
it doesn't work.
what's the common practice to solve this problem?
Use:
RewriteRule ^search/([A-Za-z0-9-]+)$ search.php?keyword=$1 [QSA,L]
QSA|qsappend When the replacement URI contains a query string, the
default behavior of RewriteRule is to discard the existing query
string, and replace it with the newly generated one. Using the [QSA]
flag causes the query strings to be combined.

Paging URL rewriting not working in php using .htaccess

I am using this rule in .htaccess :-
RewriteRule ^online-sale on-sale.php
RewriteRule ^online-sale/page-([0-9]+)$ on-sales.php?page=$1
First rule is working fine. for eg. if i call http://www.sitename/online-sale than page is opening successfully. When i am calling http://www.sitename/online-sale/page-2 than page is opening fine, but I can't access $_REQUEST["page"] value on this page.
Can anyone suggest me what is the problem? Is it possible or not?
Thanks in advance.
You need to use anchor $ in first rule to avoid matching for paging URL as well:
RewriteRule ^online-sale/?$ on-sale.php [L]
RewriteRule ^online-sale/page-([0-9]+)/?$ on-sale.php?page=$1 [L,QSA]
It is also advisable to use L and QSA flags.
QSA (Query String Append) flag preserves existing query parameters while adding a new one.

.htaccess $_GET specification

I have .htaccess code to maintain my URLs:
RewriteRule ^2014/?(?:([^/]+)/?|)(?:([^/]+)/?|)$ /data/2014/index.php?section=$1&subsection=$2 [L]
I need to modify it for cases, when i run form with get method. It makes output as (for ex.)
myweb.com/2014/about/?person=1&page=2
which I want URL to understand, I mean to get in the end hidden
myweb.com/data/2014/index.php?section=about&person=1&page=2
Thank you for any help.
You need the QSA flag:
RewriteRule ^2014/...$ /data/...&subsection=$2 [L,QSA]
^^^ here
That will append / combine the original query string to the rewritten url.

rewriterule mixed regular query and clean URL?

I am trying to get a regular query appended to any clean URL to be parsed properly. I've spent the last 3 hours trying to attempt this to no avail.
/aa/bb/?f=ff
/aa/bb/cc/dd/ee/?f=ff&g=gg
should become
a=aa&b=bb&f=ff
a=aa&b=bb&c=cc&d=dd&e=ee&f=ff&g=gg
instead I get
a=aa&b=bb
a=aa&b=bb&c=cc&d=dd&e=ee
This is my htaccess line:
RewriteRule ^([a-z]+)/([a-z]+)/?([a-z0-9]+)?/?([a-z0-9]+)?/?([a-z]+)?/?\??(.*)?$ index.php?a=$1&b=$2&c=$3&d=$4&e=$5&$6 [NC,L]
If you are wondering why I want to do this, it is to accommodate some libraries which do not support clean URLs very well.
Use the QSA flag, eg
RewriteRule ^...$ foo.php [NC,L,QSA]

How to use mod_rewrite with Zend Framework?

I've just deployed a new site using Zend Framework. Due to the popularity of my tutorials I'd like to redirect any request for a tutorial to the relevant page on the new site. So far this is what I've got:
URL before Rewrite: http://neranjara.org/tutorials/?tid=56
URL after Rewrite: http://neranjara.org/article/id/56
The .htaccess file I'm attempting to use looks like this:
$ cat html/.htaccess
RewriteEngine on
RewriteRule tutorials/\?tid=(.*)$ /article/id/$1 [R=301]
RewriteRule !\.(js|ico|gif|jpg|png|css|xml|phps)$ index.php
But this rule is not matching any URLs ... :'(
Does any one see a problem here?
Based on your previous entry:
$ cat html/.htaccess
RewriteEngine on
RewriteRule tutorials/\?tid=(.*)$ /article/id/$1 [R=301]
RewriteRule !\.(js|ico|gif|jpg|png|css|xml|phps)$ index.php
I'd suggest using this instead:
$ cat html/.htaccess
RewriteEngine on
RewriteCond %{QUERY_STRING} ^tid=([^&]*)
RewriteRule tutorials/ /article/id/%1 [R=301, L]
RewriteRule !\.(js|ico|gif|jpg|png|css|xml|phps)$ index.php [L]
BTW, this is just an example of the many things you could do using the QUERY_STRING variable in mod_rewrite. My vote goes to 'lpfavreau' since this is option #2 from their answer.
The query string (the parameters passed to your file) won't be in the RewriteRule.
Taken from http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule:
The Pattern will not be matched
against the query string. Instead, you
must use a RewriteCond with the
%{QUERY_STRING} variable. You can,
however, create URLs in the
substitution string, containing a
query string part. Simply use a
question mark inside the substitution
string, to indicate that the following
text should be re-injected into the
query string. When you want to erase
an existing query string, end the
substitution string with just a
question mark. To combine a new query
string with an old one, use the [QSA]
flag.
You have two possibilities here:
Remove your first RewriteRule and do the verification in your index.php instead before continuing to your framework. The initial query should be available in $_SERVER['REQUEST_URI'] or something like that. So verify if it's tutorials, take the tid parameter and then go on with a redirection:
header("Location: http://http://neranjara.org/article/id/$id");
exit();
Use RewriteCond with %{QUERY_STRING} instead as stated in the Apache documentation. This solution is discussed in thread like this one.
// Edit:
Have a look at Chris' answer who was kind enough to detail the solution using QUERY_STRING. This is probably what you'll want to use. Thanks Chris.
Zend uses all the htaccess power that htaccess can deliver so theres a very handy(chainable and interesting and not very well documented) method to achive this in the bootstrap!
You must use the Zend Router in your bootstrap (index.php). Probably something like: (this would be foo.com/article/23
$router =
$frontController->getRouter();
$route = new
Zend_Controller_Router_Route(
'article/:id',
array('id' => 1) ); $router->addRoute('article', $route);
More info here

Categories