htaccess redirect if one query param matches - apache

I'm trying to redirect from /somedir/index.php?action=something&id=x to /index.php?action=something&id=x
Only if action = something. id is dynamic.
Most recently I've tried this with no luck. What is wrong with this?
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} (^|&)action=something($|&)
RewriteCond %{QUERY_STRING} (^|&)id=($|&)
RewriteRule ^somedir/index\.php$ /index.php?action?something&id=%2$ [NC,R]
</IfModule>
Note: somedir has an index.php and this rule in its htaccess. Will that result in conflicts?
RewriteRule ^.*$ index.php [NC,L]

Try this rule instead of your rule in .htaccess
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} (^|&)action=something($|&)
RewriteCond %{QUERY_STRING} (^|&)id=x($|&)
RewriteRule ^somedir/index\.php$ /index.php?action=something&id=x [L,R=301]
</IfModule>
The result will not conflict.

You can use:
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} (?:^|&)(action|id)=([^&]+)&(?:.*&)?(action|id)=([^&]+)(?:$|&) [NC]
RewriteRule ^somedir/index\.php$ /index.php?%1=%2&%3=%4 [NC,L,R=301]
</IfModule>

Related

mod_rewrite subdomain (WITH a path) to URL

Struggling with this and have already done about 30+ min of Googling.
I would like to redirect:
sub.domain.com/path1 to https://anotherdomain.com/path3
sub.domain.com/path2 to https://anotherdomain.com/path4
This code below does not work but demos my approach/thinking so far
#CLIENT 1
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mockups.domain.com$
RewriteCond %{REQUEST_URI} ^/client1/
RewriteRule (.*) https://xd.adobe.com/view/xxxxxxxxxxxx/ [L,R]
</IfModule>
# CLIENT 2
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mockups.domain.com$
RewriteCond %{REQUEST_URI} ^/client2/
RewriteRule (.*) https://xd.adobe.com/view/xxxxxxxxxxxx/ [L,R]
</IfModule>
Can anyone sort me out on this?
With your shown samples only, could you please try following.
Make sure you keep these rules on TOP of your .htaccess file and keep other rules below these.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mockups\.domain\.com$ [NC]
RewriteRule ^client1 https://xd.adobe.com/view/your_new_path_here [L,R=302,NE]
RewriteCond %{HTTP_HOST} ^mockups\.domain\.com$ [NC]
RewriteRule ^client2 https://xd.adobe.com/view/your_2nd_path_here [L,R=302,NE]
</IfModule>
Also please do clear your browser cache before testing your URLs.

How to redirect link with question mark

I need to redirect:
https://www.example.com/wiki/?t=1234 to https://www.example.com/vb/showthread.php?t=1234
"1234" is hundreds of pages with different numbers
I try in .htaccess but doesn't seem to work:
RewriteCond %{QUERY_STRING} t=[0-9]
RewriteRule ^(.*)$ /vb/showthread.php?t=$1 [L]
The number may occur once or more in query string ([0-9]+)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^wiki\/
RewriteCond %{QUERY_STRING} t=([0-9]+) [NC]
RewriteRule (.*) /vb/showthread.php?t=$1 [L]
</IfModule>

.htaccess rewrite rule not working

I've been looking few different tutorials about it like
https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
and I thought it would be easy to do my own rewrite rule but I don't know why it's not working.
this is what I have in my htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp2/
RewriteRule ^lol portafolio [NC,L] # Change feed URL
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp2/index.php [L]
</IfModule>
# END WordPress
and when I write wwww.domain.com/wp2/lol it's not redirecting me to wwww.domain.com/wp2/portafolio which this code:
RewriteRule ^lol portafolio [NC,L] # Change feed URL
should do.. I guess it's not correct. Why it's not doing the rewriting correctly? I need to rewrite a bunch of URLS.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Try this code. it will help you
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

mod_rewrite not working for query string redirect

Currently I have one rewrite rule for pretty urls, which is working fine. I tried adding in a second, and it's giving me problems:
# redirect /?p=xyz to /xyz
RewriteCond %{QUERY_STRING} ^p=(.*)$
RewriteRule ^(.*)$ /index.php/%1 [L]
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
The second one is working fine, but the first one is giving me internal server error. Basically I want anything in ?p=* to go to /index.php/*
[Edit] Just to clarify, I need http://domain.com/?p=test to be equivalent to http://domain.com/test/
domain.com/test to domain.com/?p=test
Try this instead:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !p= [NC]
RewriteRule ^([^/]+)/? /?p=$1 [L,NC]
OPTION:
In case it is the opposite:
domain.com/?p=test to domain.com/test/index.php
Try this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} p=([^/]+) [NC]
RewriteRule .* /%1/index.php? [L,NC]
Remove index.php in case it is not needed, like this:
RewriteRule .* /%1/? [L,NC]

how to rewrite the url if needed with apache .htaccess

I am running a dokuwiki under http://blabla.com/ and now I have the additional url http://wiki.com/
What I want is to redirect users who request http://blabla.com/test/gna or anything else to http://wiki.com/test/gna without harmin the default .htaccess of dokuwiki
<Files ~ "^([\._]ht|README$|VERSION$|COPYING$)">
Order allow,deny
Deny from all
Satisfy All
</Files>
RewriteEngine on
RewriteBase /dev/
RewriteRule ^_media/(.*) lib/exe/fetch.php?media=$1 [QSA,L]
RewriteRule ^_detail/(.*) lib/exe/detail.php?media=$1 [QSA,L]
RewriteRule ^_export/([^/]+)/(.*) doku.php?do=export_$1&id=$2 [QSA,L]
RewriteRule ^$ doku.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) doku.php?id=$1 [QSA,L]
RewriteRule ^index.php$ doku.php
Just adding something like this at the end or beginning does not have any effect at all:
Rewritecond %{HTTP_HOST} !blabla.com/
RewriteRule ^/(.*)$ http://wiki.com/$1 [L,R=301]
Try using the below lines:
# Any URL from blabla.com will be redirected to corresponding URL on wiki.com
Rewritecond %{HTTP_HOST} ^blabla.com$
RewriteRule ^(.*)$ http://wiki.com/$1 [L,R=301]
Try removing the leading slash, apache removes it when passing URI's through rewrite rules in htaccess files:
Rewritecond %{HTTP_HOST} blabla.com
RewriteRule ^(.*)$ http://wiki.com/$1 [L,R=301]
Though you probably want to add it above your other rules so the redirected target doesn't have the doku.php?id=stuff in the URL.