I'm having an issue with conflicting quert strings on a page. for instance the page can look like the following:
website.com/photos/type/album-name/page-2/
AND
website.com/photos/type/album-name/photo-id/
the issue here is that I don't know how to make it so that it knows that when it says 'page-2' that it takes one rule, and when it's the photo-id it takes another.
I have the following
RewriteRule ^photos/([\w+.-]+)/([\w+.-]+)/([\w+.-]+)/?$ /photos.php?view=$1&slug=$2&page=$3 [QSA,L]
RewriteRule ^photos/([\w+.-]+)/([\w+.-]+)/([0-9]+)/?$ /photos.php?view=$1&slug=$2&pid=$3 [QSA,L]
Instead of having different rewrite rules for every possible query string, why don't you have a generalized rewrite and handle the query strings in your application?
Or, for your second query, try this:
RewriteRule ^photos/([\w+.-]+)/([\w+.-]+)/(page\-[0-9]+)/?$ /photos.php?view=$1&slug=$2&pid=$3 [QSA,L]
Your URLs would look like,
website.com/photos/type/album-name/page-2/ --> with the 'page-' keyword.
Related
I'm want to rewrite many pages following the simple schema:
old_page.php to content/new-page.php
I got a problem when apple.php must be rewritten to fruits/red-apple.php because I get into a rewriting loop, so i have to match domain/apple.php
I'm not sure if I can set up RewriteRule or I'll also need rewriteCond ..
You should show us what you're working with, but it sounds like your patterns are simply missing start and end of string anchors. E.g.:
RewriteBase /
RewriteRule ^apple\.php$ fruits/red-apple.php [NS,L]
You may be better off using RewriteMap if you have a lot of rules.
RewriteRule ^ihome/([^/]+) /index_ip.php?page=ihome [L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
When I go to site.local/ihome.... it goes to index.php?page=ihome.
Yet the ihome rule is above it.
I have tried it the other way around too.
I need to force ihome to a particular template file.
It's because the first rule requires a / to be present, and at least one non-/-character after that. The second rules does not (note the ? after the /. That's why browsing to ihome sends you to index.php?page=ihome.
You could modify your first rule to something like ^ihome/?([^/]+)? so the slash and characters are optional. The rule by first looks a bit strange though, as it would skip ihome/example/secondslash aswell. Is that intended?
I'd like to rewrite:
www.example.com/file.html?username=john&number=1234
To:
www.example.com/users/john
But I can't figure out how to extract the "username" value from the query string. I've been Googling this all morning and reading the official docs but no luck. I need to solve this problem with a rewrite, rather than changing the application.
Any help much appreciated!
Rangi
RewriteCond %{QUERY_STRING} username=([^&]+)
RewriteRule /?file.html /users/%1
Going to http://example.com/file.html?username=foobar will then redirect you to http://example.com/users/foobar, add an [R] to the end if you need an external redirect.
Mostly the rewrites are done the other way around, it's rare to see someone who wants a querystring in 'outside' urls but doesn't have them internally. Or did I understand your question backwards?
Ok I've solved this using two rules, although not sure if I'm doing it the best way.
RewriteRule ^file.html xxx/%{QUERY_STRING} [L]
RewriteRule ^xxx/[^=]*=([^&]*) /users$1 [R=301,L]
The first rule makes the query string part of the URL, so the second rule can see it, and therefore match and rewrite parts of it. I used "xxx" but it could be anything.
Using LAMP, is it possible to write rewrite rules to redirect URLs like the following?
http://example.com/topic/142 -> http://example.com/static/14/142.html
--Edit--
The rule is to get ID's first 2 numbers as folder name, then ID.html.
Try this rule:
RewriteEngine on
RewriteRule ^topic/(([0-9]{2})[0-9]*)$ static/$2/$1.html
Is it possible, yes, surely.
RewriteRule /topic/(.+) /static/14/$1.html
However, this will give you the /14/ part every single time. As long as you don't have a hint were this part is encoded in your original URL, there is no way to change this.
RewriteEngine on
RewriteRule ^(([0-9]{1,2})[0-9]*)$ /$2/$1.html
Greedy matching means that the first selector will pick up two characters if they are available.
However, I'm not sure that your rule makes much sense, as pages 14, 140-149 and 1400-1499 will be in the same directory. Might it make more sense to put 0-99, 100-199, etc in the same directory?
RewriteEngine on
RewriteRule ^([0-9]{1,2})$ /0/$1.html
RewriteRule ^(([0-9]+)[0-9]{2})$ /$2/$1.html
I'm trying to get all HTML and PHP files on my site to redirect through the index.php, so that they can have common frames applied to them, a templating solution I coded that I found to be quite elegant. Anywho, I'm having issues with my PHP files, specifically those that have arguments after them.
My regular rule for PHP files is the following:
RewriteCond %{REQUEST_URI} !index.php$
RewriteRule ^(.+).php$ index.php?page=$1&type=1 [NC,L]
This works fine for any pages that have no arguments, but you can see that any PHP documents that have
?argument=something
end up as:
index.php?page=path/to/page?argument=something&type=1
which is not a working solution at all. Now, what's bothering me here is the $ at the end of the rule, shouldn't that cause it to fail if there is anything after the .php?
Anywho, I tried rewriting the rule as:
RewriteRule ^(.+).php\?(.+)$ index.php?page=$1&type=1&$2 [NC,L]
but that simply doesn't trigger at all. It seems that the regex flavor used in mod_rewrite is far different than I'm used to working with, so I'm sure these are simple mistakes I've made, but I can't seem to find decent documentation for this flavor of regex other than the most basic of examples.
Can anyone show me what I'm doing wrong? Thanks.
Try qsa in your rule, which stands for "query string append" - mod_rewrite will then append any query string from the original URL to the rewritten URL
RewriteCond %{REQUEST_URI} !index.php$
RewriteRule ^(.+).php$ index.php?page=$1&type=1 [NC,L,qsa]
RewriteRule doesn't match against the query string, which is why your second attempt did not work. Here's the relevant note from the manual
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.