Trying to write a rewrite rule to capture two GET variables
http://stackoverflow.com/blogs/category/general/1
RewriteRule ^blogs/category/(.+)/?$ blogs.php?category=$1 [PT,L,QSA]
RewriteRule ^blogs/category/(.+)/([0-9]+)/?$ blogs.php?category=$1&page=$2 [PT,L,QSA]
However when I grab these from the headers it looks like this?
$_GET['category'] = "general/1";
$_GET['page'] = "";
As you can see I have two rules, one for just when they provide category and one for when they also provide page number. Might be wrong about that approach I'm not sure.
What am I doing wrong here? How can I separate these variables properly using the rewrite rules (I know I could hack it in php but that's ugly)
I think you just have to switch them so that the more specific one is handled first:
RewriteRule ^blogs/category/(.+)/([0-9]+)/?$ blogs.php?category=$1&page=$2 [PT,L,QSA]
RewriteRule ^blogs/category/(.+)/?$ blogs.php?category=$1 [PT,L,QSA]
To explain that a little bit further: All regular expressions are greedy if not specified otherwise. Which means, that the regular expression tries to get as much as possible. (.+) matches "general/1".
Related
I'm trying to wrap my tiny brain around how the .htaccess can convert my somewhat undesirable URL into a cleaner eye candy link.
This is my current, scruffy URL.
expand.php?category=Mods&subcategory=Wrestlers&faction=WWE
And what I would like it to be render as, would be something perhaps like this?
expand/Mods/Wrestlers/WWE/
Am I right in thinking this is correct syntax to perform this? Because it doesn't seem to do anything right now!
RewriteRule ^([^/]+)/([^/]+)$ expand.php?p=$1&sp=$2 [L]
I would appreciate it if some bright chap might be able to help me out of this pickle!
You can do it like this. You will need to make sure the rewrite matches the URL you wish to internally redirect too, which is expand.php?category=Mods&subcategory=Wrestlers&faction=WWE
So to take care of that you should be able to use this.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^expand/([^/]+)/([^/]+)/([^/]+) expand.php?category=$1&subcategory=$2&faction=$3 [NC,L]
Try
^expand/(\w+)/(\w+)/(\w+)?$ expand.php?category=$1&subcategory=$2&faction=$3
To internally rewrite expand/Mods/Wrestlers/WWE/ to expand.php?category=Mods&subcategory=Wrestlers&faction=WWE, you'll need to make a regex that matches that first url. Your current regex would match an url with 2 parts, but your example url has 4 parts. I am pretty sure that doesn't fit ;-)
So... how do we fix it? Well, we make a regex with 4 parts. In fact, we know that the first part needs to be equal to expand, so we end up with this:
RewriteRule ^expand/([^/]+)/([^/]+)/([^/]+)/?$ /expand.php?category=$1&subcategory=$2&faction=$3 [QSA,L]
If you add this to the .htaccess in your www-root and we now go to http://example.com/expand/Mods/Wrestlers/WWE/, we should see whatever expand.php outputs with those parameters.
I am building a site with a wide, wide variety of optional $_GET[] parameters: my url can range anything from
index.php?mod=reg to
index.php?mod=complicated¶m1=foo or
index.php?mod=EvenMoreComplicated&w=4&x=1&y=2&z=3.
I understand that, in a way, this could be handled with a whole bunch of rewrite rules with an [L] parameter, however, isn't there an easier way to handle this without having to manually specify each possible $_GET[] parameter?
In many frameworks, this issue is tackled by one unique rewrite rule :
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
That means that you get all the query string in one single parameter $_GET['url']
Then, parse_str() function in :
parse_str($_GET['url'], $queryArgs);
This will give you the $_GET equivalent in $queryArgs.
Also, this is demo code, don't forget sanitization/filtering in real code.
Hope this helps !
I have the following rule:
RewriteRule ^(image/[0-9]*/(.*))?$ image.php?prettyUrl=true&nav=$1 [QSA,NC,L]
This is supposed to replace
http://mysite.com/image/12345/imagename.png => http://mysite.com/image.php?pretyUrl=true&nav=image/12345/imagename.png
For the particular case where the pretty url contains '/image/' the $_GET array is empty. If I use a different name like 'images' or 'asdfg' the $_GET array will contain both pretyUrl and nav.
Any idea why?
Thanks!
If the rewrite rule must work for every URL in image/, (and the /1234/ digits part is not required), then this may be what you are looking for:
RewriteRule ^(image(/.*)?)$ image.php?prettyUrl=true&nav=$1 [QSA,NC,L]
If you can phrase your question a bit more specifically, a better solution can be refined.
I'm struggling with a redirect problem. I need to redirect the following URL with mod_rewrite thorough .htaccess
http://www.mysite.com/somescript.php?&lang=php&var=1&var=2
to the following
http://www.mysite.com/somescript.php?lang=php&var=1&var=2
So, basically I just need to remove the
&
before
lang=php
However, the order is important. Sometimes
&lang=php
appears after other variables in the querystring. in this scenario I need the
&
to remain part of
&lang=php
Is this possible?
To summarise, if &lang=php appears at the beginning of the query string, remove the &. If &lang=php appears anywhere else in the query string, the & must remain.
Hope this is clear!
I would change the script myself but unfortunately I am not the developer, and he doesn't seem too helpful at the moment; this is a quick fix.
I would replace ?& with ?:
RewriteCond %{QUERY_STRING} ^\&(.*)$
RewriteRule ^somescript\.php$ /somescript.php?%1 [L,R=301]
why don't you match "?&" and replace it by "?" ?
Something like:
RewriteRule ^(.*)?&(.*) $1?$2 [L]
(not tested)
Because I think the combination "?&" is never valid...(?)
Though .htaccess, I want to redirect /page/var1/var2 to ./page.php?var1=var1&var2=var2. This is very easy BUT I want to make it so /page/var1 also redirects to ./page.php?var1=var1 (without having the var2). The only way I'm doing this is:
RewriteRule page/(.*)$ ./page.php?var1=$1
RewriteRule page/(.*)/(.*)$ ./page.php?var1=$1&var2=$2
As you can see it's very redundant, and I have a ton of lines like this, so it gets very messy. Any way to define "optional" parts?
The expression .* matches both var1 and var1/var2 thus the first rule is applied on both.
So you have to specify it that the first rule only matches var1. This can be done by replacing . (any character) by [^/] (any character except /). So try this:
RewriteRule ^page/([^/]+)$ ./page.php?var1=$1
RewriteRule ^page/([^/]+)/([^/]+)$ ./page.php?var1=$1&var2=$2
Edit You can also write this in one rule:
RewriteRule ^page/([^/]+)(/([^/]+))?$ ./page.php?var1=$1&var2=$3
mod_rewrite is not well suited to such heavy lifting, leave that to your PHP app.
RewriteRule page/(.*)$ ./page.php?vars=$1
and somewhere near the beginning of page.php:
<?php
$vars = explode('/',$_GET['vars']);
Voila, you have an array of your vars; now you could do some processing there to see what is required/optional for your app and react accordingly.
The best approach is to just hand everything over to your PHP script.
I wrote an article on how to do friendly URLs, which covers this in more detail.