apache rewrite rules - apache

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

Related

How to add redirects to pages with a query string?

I may be wrong in this but i'm seeing loads of answers for appending to a query string when using .htaccess for redirects but wondered if there was a clear way of achieving the following:
RewriteRule ^old-webpage/$ http://newwebsite.com/intro?redirect=oldsite [R=301,L]
So the old website doesn't have query strings and i'd like to be able to redirect people to the new site with a query string in place so I can adjust a few things and show a message.
This will work in redirecting, however now I'm getting the domain:
http://newwebsite.com/intro?redirect=oldsiteoldweb-page/
so is there something missing in the Rewrite rules that stops the old-webpage being concatenated to the query string after it's redirected?
Your above example didnĀ“t work for my System so I suggest you following working conditions & rules:
Option 1 - dynamic param
RewriteCond %{HTTP_HOST} ^olddomain.com$
RewriteRule ^(.*)$ http://newwebsite.com/intro?redirect=%{HTTP_HOST} [R=301,L]
Version 2 - static parameter
RewriteCond %{HTTP_HOST} ^olddomain.com$
RewriteRule ^(.*)$ http://newwebsite.com/intro?redirect=oldsite [R=301,L]

.htaccess rewrite urls with parameters

I am trying to rewrite my urls through a .htaccess file to make them more clean looking. I have
http://localhost:801/Test/test.php?school=19&name=Greenhaven-Elementary
and it needs to end up looking like
http://localhost:801/Test/test.php/19/Greenhaven-Elementary
In my .htaccess file I have the following
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/+]+)([0-9]+)$ test.php?school=/$1&name=$2/ [L]
I have tried other ways but being new at using .htaccess files I haven't been able to figure it out.
This should do what you're after:
RewriteEngine On
RewriteCond %{QUERY_STRING} school=(.+)&name=(.+) [NC]
RewriteRule ^(.*)$ http://localhost:801/Test/test.php/%1/%2? [R=301,NC,L]
So what does the above do?
First, it will take the query school= and name= as a condition, if this condition is met then it will grab any version of the variables using (.+).
It will then rewrite the URL using 301 redirection to show http://localhost:801/Test/test.php/anything/anything2. The use of %1 and %2 is to grab the variables from school= / name= and then we use ? to stop the original query string from appearing on the end of the newly rewritten URL.
Make sure you clear your cache before testing this.
EDIT:
I wrote this for the singular query:
RewriteEngine On
RewriteCond %{QUERY_STRING} item=(.+) [NC]
RewriteRule ^(.*)$ http://localhost:801/Test/%1? [R=301,NC,L]
This includes removing test.php and on my server works without issue and returns http://localhost:801/Test/anything

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

apache rewrite rules for locale specific pages

I want to redirect users to a generic registration page if they come in without a locale in the URL, and specific registration page if they come in with a locale. For some reason the below doesn't work, let me know how to fix:
RewriteCond %{QUERY_STRING} source=actp
RewriteRule ^/(en|de|fr|zh|ja|jp|ko|kr)/web/login/registration $1/web/login/registration? [R=301,L]
RewriteCond %{QUERY_STRING} source=actp
RewriteRule /web/login/registration /web/login/registration? [R=301,L]
example:
Before: website.com/web/login/registration
after: website.com/web/login/registration?source=actp
before: website.com/kr/web/login/registration
after: website.com/kr/web/login/registration?source=actp
Based on your before/after URL's, you are using the RewriteCond wrong here. It would have expected the query string source=actp to match before even evaluating the RewriteRule. Since your before URL has no such query string, the RewriteRule will not even be evaluated. You don't need a RewriteCond here at all.
Try this:
RewriteRule ^/?((en|de|fr|zh|ja|jp|ko|kr)/)?web/login/registration$ /$2web/login/registration?source=actp [R=301,L]

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