Odd behavior adding RewriteRule in .htaccess - php

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.

Related

mod rewrite rule not passing key values

I have a few different mod-rewrite rules working, but this last one refuses to pass the 3 parameters to my script (index.php)
I do get the p=value, but the id, rid and chk vars don't even get defined...
A URL might look like this . . . .
http://www.domain.com/pagename.htm?id=29&rid=174&chk=a9cdca614135bbef2fb1f2bedf171f61
The rule...
RewriteRule ^/pagename\.htm\?id\=([0-9]+)&rid\=([0-9]+)&chk\=([a-f0-9]{32})$ /index.php?p=pagename&id=$1&rid=$2&chk=$3 [L]
Output of print_r($_REQUEST);
Array ( [p] => pagename )
I simply can not understand why this does not work..
Like #MarcB said, rewrite rules don't include the query string and you have to use a RewriteCond to check it. This is what would work for you based on your example above:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)&rid=([0-9]+)&chk=([a-f0-9]{32})$
RewriteRule ^pagename.htm index.php?p=pagename&id=%1&rid=%2&chk=%3 [L]
Or like I said above, you can also use the QSA flag like:
RewriteRule ^(pagename).html index.php?p=$1 [L,QSA]
And that will append any additional query string on to index.php, but doesn't validate it (which should REALLY not be done in mod_rewrite). It also allows you to add additional parameters, doesn't require a change of the rules to accommodate them and won't break if the key/values are in different order or case. Note: I added parenthesis around pagename and used the match $1 in the rewritten url. This is so it is easier to change the page name for multiple rules because you don't have to change it in two places.

how to make .htaccess ignore specific uri part

I don't know how to make a rewrite condition to obtain the right behaviour.
This is the link i have:
http://www.example.com/variable/Known/Uri/ecc
or
http://www.example.com/variable
I'd like to ignore the "variable" part and consider only the rest of the uri.
I will consider "variable" only when reading $_SERVER["REQUEST_URI"].
I think what you want to do is:
RewriteEngine On
RewriteRule ^[^/]+(.*)$ $1 [L]

Confusion with .htaccess

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.

Rewrite .htaccess 2 GET variables not splitting?

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".

.htaccess with php hidden parameters and language selection

I can't seem to get my .htaccess file to route the urls to my site correctly. I have a number of languages people can choose from wanting URL's like:
http://www.domain.com/en/
http://www.domain.com/en/contact
But I can't seem to get the page 'contact' working when writing a rule to get the 'en' variable.
RewriteRule /([^/]+)/([0-9]+)/ index.php?language=$1
I use that to grab the language code but how could I get the contact page to work?
EDIT:
Apparently I needed some QSA option but now the language get variable grabs contact as the variable with the en
RewriteRule ^(.*)$ index.php?language=$1 [QSA,L]
With this rule the site:
http://www.domain.com/en/contact
Returns:
en/contact
EDIT2
What I am trying to accomplish is the directory structure:
/
/contact
/about
Having these folders in the root but grabbing and ignoring the /en/ language variable. So I don't need a second variable for &page=contact, I need it to route into the directory folder.
Try combining your two expressions, although you need to modify the second group - [0-9]+ will only match numbers, not words like contact. Try this:
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?language=$1 [QSA,L]
The QSA option allows a query string to be appended to the clean URL, perhaps something like this:
http://www.domain.com/en/contact?to=support&subject=Hello
In response to your comment, this expression should do the trick:
RewriteRule ^([^/]*)/([^/]+)/?$ $2/index.php?language=$1 [QSA,L]
In the rewritten rule, $2 holds contact, for example, and $1 holds en. The former is used as the directory, and the latter as an argument in the query string.

Categories