Reoccurring htaccess command - php

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.

Related

.htaccess ReWrites cancelling each other out ... Any Ideas?

I have a couple of rules in my .htaccess file in order to make the URLs a bit cleaner, however, they seem to be cancelling each other out.
The first rule is just to remove the .php from page names,
example : mysite.com/join rather than mysite.com/join.php
RewriteEngine On
Rewrite Condition : %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
The second rule is to make it easier for Users to share their profiles on my site,
example : mysite.com/user1 rather than the actual URL mysite.com/profile.php?user=user1
RewriteRule ^([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]
I've been playing round with them, and they essentially cancel each other out - Any ideas on how I can get them both working?
Thanks
The problem you have is that both conditions are almost identical i.e. anything that ^([_A-Z0-9a-z-+]+)$ matches will also be matched by ^([^.]+)$, so someone accessing mysite.com/user1 will get redirected to mysite.com/user1.php since that is the first rule that is encountered, and it has the L flag to prevent processing more rules. To prevent this happening you need to make the rules different, e.g. perhaps require user pages to be mysite.com/users/user1? Then you could write the rules as
RewriteRule ^([^./]+)$ $1.php [NC,L]
RewriteRule ^users/([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]
Note that you need to add / to the characters not to be matched in the first rule, otherwise it will still match mysite.com/users/user1.
Edit
A couple of other alternatives:
If you were willing to have actions (e.g. join) use URLs such as mysite.com/action/join then you could keep users at the top level e.g.
RewriteRule ^action/([^.]+)$ $1.php [NC,L]
RewriteRule ^([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]
Or if you know the names of all your actions you could put them in an alternation (this would require that you couldn't have a user called e.g. join):
RewriteRule ^(join|login|logout|delete)$ $1.php [NC,L]
RewriteRule ^([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]

URL rewrite php query strings

I have two urls doing some queries which I'm struggling to rewrite in the manner I want.
First
/shop/index.php?category=catslug
which I want to be
/shop/category
Second
/shop/index.php?product=slug
to
/shop/category/product
I have this currently:
RewriteRule ^shop/([A-Za-z0-9-]+)/?$ shop/index.php?category=$1 [L]
RewriteRule ^shop/[A-Za-z-]+/([A-Za-z0-9-]+)/?$ shop/index.php?product=$1 [NC,L]
The problem is that one of the rules is ruining anything that starts with shop so that things like shop/cart doesn't work. I'm so confused. Is this possible?
Note that for shop/cart, the cart part matches the [A-Za-z0-9-]+ part of the first RewriteRule. So it is rewritten to shop/index.php?category=cart.
The way to avoid this, hoping that you have a relatively small number of fixed URLs, is to have a RewriteRule before your 2 rules like
RewriteRule ^shop/(cart|this|that|other|thing) - [L]
RewriteRule ^shop/([A-Za-z0-9-]+)/?$ shop/index.php?category=$1 [L]
RewriteRule ^shop/[A-Za-z-]+/([A-Za-z0-9-]+)/?$ shop/index.php?product=$1 [NC,L]
For requests matching one of the pipe-delimited strings (such as cart), - means don't change the request, and [L] means last (don't continue to the next rules).

How to automatize rewriterule in .htaccess?

I'm doing this in .htaccess:
RewriteRule ^([A-Za-z0-9-]+)[/]?$ /index.php?u1=$1 [L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)[/]?$ /index.php?u1=$1&u2=$2 [L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)[/]?$ /index.php?u1=$1&u2=$2&u3=$3 [L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)[/]?$ /index.php?u1=$1&u2=$2&u3=$3&u4=$4 [L,QSA]
Is there any way to make this automatically from u1 to u(infinite), automatically, based on the length of the url, without having to define every case?
No, neither Apache nor regex offer programmatic processing of an unspecified number of arguments. But PHP is designed for such things, so you're best off simply using one rule:
RewriteRule ^([A-Za-z0-9/-]+)$ /index.php?path=$1
Then have your PHP script break the path variable apart by calling the explode function on the forward-slash character. And you'll get an array containing each piece of the full path.
This way your PHP script can handle an unlimited number of path elements, and Apache won't need to wear itself out trying to make sense of infinite regex patterns.

Combining multiple htaccess rewrite rules into one

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().

Query Strings & Mod ReWrite

I'm not too inexperienced with ReWrite (not a master either, though) so I was hoping somone might be able to help me.
RewriteRule ^$ index.php?page=home [NC]
RewriteRule ^adm$ index.php?page=adm_home [NC]
RewriteRule ^adm/stats index.php?page=adm_stats [NC]
Above is a snippet of my .htaccess file. As you can see, when someone visits http://www.example.com/adirectory/ it actually calls on index.php?page=home, similarly if someone goes to http://www.example.com/adirectory/adm/ it will still call index.php?page=adm_home within the "adirectory".
What I'm wanting to achieve is this: I want to be able to display alerts on my pages, and to do this I want to simply be able to add alert=n (where n is a number) and thus have the redirect as index.php?page=home&alert=n
However, I can't understand how this can be done, regex is confusing me. Seeking your help.
You can set the QSA flag to automatically append the originally requested query string to the new one:
RewriteRule ^$ index.php?page=home [L,QSA]
RewriteRule ^adm$ index.php?page=adm_home [L,QSA]
RewriteRule ^adm/stats$ index.php?page=adm_stats [L,QSA]

Categories