Combining multiple htaccess rewrite rules into one - php

First, let me say this: I suck at regex and htaccess. I'm trying to make a rule that will properly parse url segments into variables. So far I have this:
RewriteRule ^([^/]+)/?$ index.php?query[]=$1 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?query[]=$1&query[]=$2 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?query[]=$1&query[]=$2&query[]=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?query[]=$1&query[]=$2&query[]=$3&query[]=$4 [QSA,L]
It works, sort of, but I feel it's longer than it needs to be; and what if I want 5 or 6 or 7 variables? Is there a more condensed way to write this out?
Also, when I spit out the query array, the first element is always index.php. What's up with that?

Don't use RewriteRule to convert pathinfo to query parameters, but just enable MultiViews in Apache HTTP Server and use $_SERVER['PATH_INFO'] in index.php with little help of explode().

Related

htaccess 3 different urls in one php file

I have 3 step in one php file:
This is my htacess now:
RewriteRule ^igra/(.*)$ "/index.php?page=igra&id=$1"
RewriteRule ^igra/(.*)/sezona/(.*)$ "/index.php?page=igra&id=$1&season=$2"
RewriteRule ^igra/(.*)/sezona/(.*)/liga/(.*)$ "/index.php?page=igra&id=$1&season=$2&league=$3"
When i go in browser something like index.php?page=igra&id=$1 or index.php?page=igra&id=$1&season=$2 or index.php?page=igra&id=$1&season=$2&league=$3 sure with real values it works fine, but when i try to access with this pretty links it always show me the first rewrite rule..
I hope u understand me what i need here, best regards..
Your first rule is capturing everything, so the subsequent rules never get executed. Just switch them around:
RewriteRule ^igra/(.*)/sezona/(.*)/liga/(.*)$ /index.php?page=igra&id=$1&season=$2&league=$3 [L]
RewriteRule ^igra/(.*)/sezona/(.*)$ /index.php?page=igra&id=$1&season=$2 [L]
RewriteRule ^igra/(.*)$ /index.php?page=igra&id=$1 [L]
Notice also the addition of the L flag.

How to rewrite specific URL using htaccess

I've been breaking my head on this for quite some time and I don't see the solution.
I want to rewrite a URL with a GET language parameter to a more clean URL.
For instance:
http://www.example.com?lang=en
Needs to be:
http://www.example.com/en
The above works fine with this rewrite rule:
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L]
But I can't get it to work on URLs like these:
http://www.example.com/contact.php?lang=en
http://www.example.com/about.php?lang=en
That need to be:
http://www.example.com/en/contact.php
http://www.example.com/en/about.php
Anyone have an idea what I'm missing in my rewrite rule to make this work?
You will need an additional rewrite rule for handling /en/about.php:
RewriteEngine On
RewriteRule ^(en|nl|fr|de)/([\w-]+\.php)$ $2?lang=$1 [L,QSA]
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L,QSA]

PHP/Apache rewrite rules with multiple parameters?

I got the difficult when rewrite rules with multiple parameter,to modify a URL to SEO-friendly.
My URL:
http://domain/cat.php?alias=canon&sort=price&page=3
I want to have a rewrite rule so that the following:
http://domain/c/canon?sort=price&page=3
Heres my current rule:
RewriteEngine On
RewriteRule ^c/([a-z,0-9-]+)$ cat.php?alias=$1 [L]
RewriteRule ^c/([a-z,0-9-]+)?sort=([a-z]+)$ cat.php?alias=$1&sort=$2 [QSA]
RewriteRule ^c/([a-z,0-9-]+)?sort=([a-z]+)&page=([0-9]+)$ cat.php?alias=$1&sort=$2&page=$3 [QSA]
I try to get the params but it doesn't work. Anyone have any ideas on which rewrite rules to use?
Thank you!
--hatxi
RewriteRule ^c/([a-z,0-9-]+) cat.php?alias=$1 [L,QSA]
Should be enough. The QSA flag will take care of passing the sort and page parameters.
Your rules don't work because of the [L] flag on the first one, it just discards the rest because it always matches first.

Reoccurring htaccess command

I need to continue this indefinatly in length without writing it out everytime.
Is there a way to achieve this?
RewriteRule ^(\w+)/?$ index.php?page=$1 [QSA]
RewriteRule ^(\w+)/(\w+)/?$ index.php?page=$1&$1=$2 [QSA,L]
RewriteRule ^(\w+)/(\w+)/(\w+)/?$ index.php?page=$1&$1=$2&$2=$3 [QSA,L]
So all the way through $1=$2 then $2=$3 so page=blog&blog=article&article=date&date=2012
My PHP then takes this and runs analysis on this. So could be as as $1 -> $11 Long URL I know means that I can still enable adding.
AAA.com/blog/article/date/2012/?colour=blue
Not as such. To get an replacement $5 placeholder, you do need a match group.
One could of course optionalize it group-wise:
RewriteRule ^(\w+)(?:/(\w+))?(?:/(\w+))?(?:/(\w+))?(?:/(\w+))?(?:/(\w+))?(?:/(\w+))?/?$ index.php?page=$1&$1=$2&$2=$3&$3=$4&$4=$5&$5=$6 [QSA,L]
But then you had a few &=&=&= trailing for absent entries. Usually it should result in an empty $_GET[""], but PHP even ignores that. So, might be an option. I don't feel it's a more readable or concise RewriteRule though.

How do urls without get parameters work

Hi i have come across some urls of the type "http://localhost/jsfweb/cat/query/" where query is a string that will return some results from a mysql database. I am familiar with urls of the type "http://localhost/jsfweb/cat.php?query=query" how can i use those urls with php?
You can do this by rewriting url in htaccess file by this :
RewriteRule ^cat/(.*)$ cat.php?query=$1
Use modrewrite
You do something like this:
Inside a file named '.htaccess'
Options +FollowSymlinks
Options -MultiViews
RewriteEngine on
RewriteBase /php/eclipse/ShiftPlus2/
#forbidden area
#RewriteCond %{THE_REQUEST} index\.php
#RewriteRule ^index\.php$ http://localhost/php/eclipse/ShiftPlus2/? [R=301]
#unique case
RewriteRule ^email$ email.html [L]
#general case
RewriteRule ^([a-zA-Z_]*)/?$ index.php?query=$1 [L]
RewriteRule ^([a-zA-Z_]+)/([a-z]+)/?$ index.php?query=$1&action=$2 [L]
RewriteRule ^([a-zA-Z_]+)/(-?\d+)/?$ index.php?query=$1&id=$2 [L]
RewriteRule ^([a-zA-Z_]+)/([a-z]+)/(-?\d+)/?$ index.php?query=$1&action=$2$id=$3 [L]
RewriteRule ^([a-zA-Z_]+)/(\w+)/?$ index.php?query=$1&special=$2 [L]
#RewriteRule ^index.php$ login [R]
Where on the left side, there is a Rewrite rule with a regular expression and on the right, this is the real link like you know.
Take a look at Apache's mod_rewrite. For most of the websites it is done through that module. If you don't want to get your hands dirty with it, you can employ some sort of MVC framework that incorporates it.
Apache mod_rewrite is what you want. This is an excellent read for beginners:
10 Mod Rewrite Rules You Should Know
CodeIgniter (MVC framework) works by accepting a single entry point to the application, then routing according to what follows. So say you declare index.php as the default document, then /index.php/controller/view is the same as /controller/view. The parameters controller and view are used to instantiate and run the appropriate classes.

Categories