Apache RewriteMap with URL's that may or may not contain a query string - apache

We have a RewriteMap that contains the following examples:
/home /
/index?q=video /videos
/index?q=media /media
/epic.html /epic
How can we RewriteMap to match urls that may or may not have a QUERYSTRING attached to it?
We are currently using:
RewriteMap mappings txt:/data/redirect_mappings
RewriteCond ${mappings:$1|Unknown} !Unknown
RewriteRule ^(.*)$ ${mappings:$1?%{QUERY_STRING}|$1?%{QUERY_STRING}} [L,R=301,QSD]
However, it does not seem to be taking any effect. What am I doing wrong?

Give this a shot
RewriteMap mappings txt:/data/redirect_mappings
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} (.*)
RewriteCond ${mappings:%1|NOTFOUND} !NOTFOUND
RewriteRule ^(.*)$ ${mappings:%1}? [L,R=301]
RewriteCond ${mappings:$1|NOTFOUND} !NOTFOUND
RewriteRule ^(.*)$ ${mappings:$1} [L,R=301]

Related

Apache redirect using RewriteEngine with and without query string

I'm trying to get 2 redirections set on my htaccess file but I can't make it to work.
The desired output is:
if the user goes to www.mywebsite.com/page1, it gets redirected to /folder/newPage
if the user goes to www.mywebsite.com/page1?a=123, it gets redirected to /folder/anotherPage?a=123
I tried this
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/page1$ /folder/newPage [R=301,L]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^/page1(.*)$ /folder/anotherPage$1 [R=301,L]
I also tried something like this:
RewriteCond %{REQUEST_URI} ^/page1$
RewriteRule ^(.*)$ /folder/newPage [R=301,L]
RewriteCond %{REQUEST_URI} ^/page1$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ /folder/anotherPage%1 [R=301,L]
Note that Rewrite mod is turned on on my apache.
Any idea how to make it work?
Check through the following:
RewriteEngine On is present before all the rules
AllowOverride directive allows your htaccess files to load
htaccess receives URLs without the leading slashes, unlike server config files, or VHosts file.
.* matches 0 or more characters, thus %{QUERY_STRING} ^(.*)$ also matches empty query strings. Change this to .+.
Final rules should be:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^page1$ /folder/newPage [R=301,L]
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^page1$ /folder/anotherPage?%1 [R=301,L]

Change Rewrite rule depending on the URL

I have a domain example.com which currently points to example123.com. I only want to hide the index.php file when I am at example.com, not example123.com. I'm on Apache 2.2 so I can't use the <if> statement.
How can I possibly do it?
I currently have:
RewriteCond %{REQUEST_URI} ^example.com.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
But this doesn't work because this can take away the index.php from both domains. I only need to do that with one.
You can use the following code in you htaccess :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteRule ^(.*)$ /index.php?/$1 [NC,L]

Simple Apache mod_rewrite remapping

I have to remap a few ID's to URL strings (301 redirect) and I have to do it with mod_rewrite:
/page.php?id=15 to /pagexy
/page.php?id=10 to /pageyz
The rule:
RewriteRule ^page.php?id=15$ /pagexy [L,R=301]
doesn't work. What am I doing wrong?
You need to inspect the query string separately. The following should work:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=15\b [NC]
RewriteRule ^/page.php$ /pagexy? [L,R=301]
RewriteCond %{QUERY_STRING} id=10\b [NC]
RewriteRule ^/page.php$ /pageyz? [L,R=301]
Given you are tying ids to pages, it may also pay to look at using a RewriteMap

apache rewritemap not working URL does not translate

I cannot get my rewrite map to work. I have read a lot of pages on this and yet can't figure out how.
I have the following in my httpd.conf and took an apache restart
RewriteMap ErrorMap txt:/usr/local/apache/conf/userdata/std/1/techencl/site.com/vb2xf.map
The rewrite map has the following entry (edited after the tip in the first reply)
corner/lock-unlock-140859.html http://www.site.com/com/ts/lock-unlock.70898/
The .htaccess in the root directory has the following entry
RewriteCond ${ErrorMap:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*)$ ${ErrorMap:$1|$1} [R=301]
Edit: Just posting my current .htaccess file contents for reference
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule [^/]+/.+-([\d]+)/p([\d]+)/ showthread.php?t=$1&page=$2 [NC,L]
RewriteRule [^/]+/.+-([\d]+)/ showthread.php?t=$1 [NC,L]
Options +FollowSymlinks
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
RewriteCond ${ErrorMap:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*)$ ${ErrorMap:$1|$1} [R=301]
</IfModule>
When you call the map:
${ErrorMap:$1
The $1 is never going to have http://www.site.com/ in it. It will only have this part:
corner/lock-unlock-140859.html
That means in your map, you need to make it look like this:
corner/lock-unlock-140859.html http://www.site.com/com/ts/lock-unlock.70898/

Redirect Subdomain to new domain

Hi guys trying to get a 301 redirect working and having trouble. I need to redirect sub.domain1.com to www.domain2.com and make sure that any file names or parameters get sent over with it.
This is what I was trying:
RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
I also tried this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^sub\.domain1\.com$ /www.domain2.com? [R=301,NE,NC,L]
Where am I messing up?
You missed the subdomain part and proper escaping.
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
Further explain can be found in this question.
Rule of thumb for rewriterules: from the most complex to the less complex.
And don't forget the QSA directive (QSA = Query String Append = "make sure that any file names or parameters get sent over with it")
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [QSA,R=301,L]
Tell me if it works.