.htaccess rule not catching - apache

I'm trying to catch http://mysite.loc/orders/invoice/?id=asdf and redirect it, but it's not catching. Does anyone have any ideas on something I might've missed?
RewriteEngine On
RewriteRule ^orders/invoice?id=([^/]+)$ /store/order/view?hash=$1 [R=301,L,NC]

Rewrite rules act on request URIs. The query string (the question mark and everything after it) is not part of the URI so you can't write a pattern to match it.
Try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=(.*)
RewriteRule ^orders/invoice /store/order/view?hash=%1 [R=301,L]

Related

Rewrite Rule - need guidance

I need to do some redirecting to get some internal links to work but I'm having a complete block.
The url would be http://www.something.com/faqs/What_happens_if_I_move_home?
redirected to http://www.something.com/faqs/index/What_happens_if_I_move_home?
but it must look like the original url. I'm sure there is a simple answer but rewrite rules and regex are a mystery to me at times.
I did try RewriteRule ^faqs(/.*)?$ /faqs/index$1 [R,L,NC]
amongst many others!
try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/faqs/index
RewriteRule ^faqs/(.*) /faqs/index/$1 [L,NC]

Apache .htaccess rewrite parameter to directory

I've got an application that has been migrated to a newer platform. The tasks are similar and I'd like to redirect a GET parameter to a directory. For example
http://gallery/index.php?gal=ABC => http://photos/gal/ABC
and
http://gallery/?gal=DEF => http://photos/gal/DEF
and the anything that doesn't get caught redirect it to http://photos
I've tried
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^(/index.php)$ /%1/%2?
However all I get is a 404 and no redirection. Similarly, I've tried
RedirectMatch ^/index\.php\?=gal(.*) http://photos/gal/$1
but I'm having trouble escaping the ? in the original URL.
What would be the proper way of going about this?
Create a .htaccess file and insert the following code:
RewriteCond %{QUERY_STRING} ^(.+)=(.+)$
RewriteRule ^index.php$ http://photos %1/%2? [L,NC,R=301]
Your order is reversed. Rewrite it in front of you
RewriteRule /(.+)\/(.+) index.php?$1=$2
The question is old but might be still relevant for others, so I suggest a slightly different general approach:
RewriteEngine On
RewriteCond %{QUERY_STRING} key=([0-9a-zA-Z]+) [NC]
RewriteRule (.*) /%1? [R=302,L]
Notes:
QUERY_STRING in the condition checks for a param name "key" and catches it's value
The rewrite rule adds the param value as directory using %1. Note the ? removes the original query part from end result

Remove Page Number from URL with .htaccess

I need page numbers from URLs of the form:
http://mydomain.com/index.php?showtopic=XXXX&page=XXXX&#entryXXXX
so they become
http://mydomain.com/index.php?showtopic=XXXX&#entryXXXX
where XXXX are integers
I've previously tried:
RewriteEngine on
RewriteRule ^(.*)showtopic=([0-9]+)&page=([0-9]+)(.*) http://mydomain.com/index.php?showtopic=$1$3 [QSA,L,R=301]
but to no avail. So I shortened it to:
RewriteEngine on
RewriteRule ^(.*)&page=([0-9]+)(.*)$ $1&$3 [QSA,L,R=301]
but still nowt. Is there anything wrong with the regex at all?
You can't match against the query string in a rewrite rule, you need to match against the %{QUERY_STRING} var inside a rewrite condition:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^showtopic=([^&]+)&page=([^&]+)(&.*)?$
RewriteRule ^index\.php$ /index.php?showtopic=%1%3 [L,R=301]
The #entryXXXX part of the URL is a fragment, and the server actually never sees that. It's a client/browser-side only thing. Hopefully, the browser is smart enough to re-append the fragment after getting redirected.

apache rewrite rules

i'm trying to get the following url to be rewritten
http://blog.mywebsite.com:8080/wp-admin/users.php?paged=2
as
http://mywebsite.com/blog/wp-admin/users.php?paged=2
i have tried a variety of rewrite conditions and rules
RewriteCond %{QUERY_STRING} ^(.*)$ [NC]
RewriteRule blog.mywebiste.com(.*)$ mywebsite.com/blog/$1
i seem to loose the query string when id do this
RewriteRule ^/wp-admin(.*) http://mywebsite.com/blog/wp-admin$1
this doesn't seem to catch it at all
also i have the rewrite log on. When does this get written to?
It should work without leading forward-slash:
RewriteRule ^wp-admin/(.*) http://mywebsite.com/blog/wp-admin/$1

mod_rewrite weird problem

I have a strange problem with mod_rewrite, the rules that are relevant here are:
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/igre\-(.*)\.php$ game.php?GameUrl=$2&Page=1 [L]
And a corresponding URL might look something like this:
example.com/miselne-igre/igre-shirk.php?Page=2
example.com/miselne-igre/igre-shirk.php
The problem is that the first rule has no effect. If I use the first URL from the example I always get 1 into the Page variable, which shows that the second rule is used.
So what's wrong with the first one? And why is the second rule even matching a URL with ".php?Page=XYZ" at the end, if I said that the URL ends with ".php"?
ps: other rules in the .htaccess file are working fine...
The query string is not part of the URI path that is being processed by the RewriteRule directive. You have to use the RewriteCond directive to process the query string.
RewriteCond %{QUERY_STRING} ^Page=[0-9]+$
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&%0 [L]
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&Page=1 [L]
But you can still simplify this by using the QSA flag (query string append):
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1 [L,QSA]
mod_rewrite is not using the query in it's rewriting process. Therefor you first RewriteRule is ignored. You could combine it with a RewriteCond (haven't tested it though) like so:
RewriteCond %{QUERY_STRING} Page=([0-9]+)
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2 [L, qsappend]
# qsappend appends the original query, in this case (Page=xx)
Ah, like Gumbo said; you can also use %1 to back reference to the page numer.
Is it just me or are your arguments back-to-front?
Do you mean:
RewriteRule ^(.*)\/(.*)\-igre\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/(.*)\-igre\.php$ game.php?GameUrl=$2&Page=1 [L]
You wanted to match miselne-igre not igre-miselne.
Obviously this doesn't address the main issue, but thought I'd throw that in.
Dom