Regex url redirect in apache - regex

i want to redirect pages in apache, so i tried this :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/?page_id=671 http://letempspourtoit.fr/news [L,R=301]
</IfModule>
but it just redirect to the home page. So i tried to escape the question mark like this :
RewriteRule ^/\?page_id=671 http://letempspourtoit.fr/news [L,R=301]
no success...
Any hint greatly appreciated!

RewriteRule works on URL-paths, and query strings are not considered part of the path. You need a RewriteCond on QUERY_STRING:
RewriteCond %{QUERY_STRING} ^page_id=671$
RewriteRule ^/$ http://letempspourtoit.fr/news [L,R=301]
As you can see, the path for the RewriteRule is /.
Your RewriteRule worked for URLs like http://host/%3Fpage_id=671, that is: URLs with a %-encoded question mark.

Related

Redirect Query String from Site Root to Sub-Domain Site Root

I'm trying to redirect
http://example.com/?download_invoice=true&attendee_id=28916&r_id=504-55803a183a0cf
to
http://secure.example.com/?download_invoice=true&attendee_id=28916&r_id=504-55803a183a0cf
but aren't having any luck. I'm able to redirect Query Strings from sub-directories just fine using this syntax:
RewriteCond %{REQUEST_URI} ^/sub-directory/$
RewriteRule ^.*$ http://secure.example.com/sub-directory/ [L,R=301]
but can't figure out how to do it from the site root. Any help would be greatly appreciate, thanks in advance!
You need to match host name in RewriteCond and pattern ^$ in RewriteRule:
RewriteCond %{QUERY_STRING} .
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$
RewriteRule ^/?$ http://secure.%1/ [L,R=301]
QUERY_STRING gets forwarded automatically to target URL.

Redirection loop (URL rewriting)

I'm trying to rewrite my website URLs. I have:
http:// website.com/v2/message.php?ID2=123
I want:
http:// website.com/v2/message-123.php
I tried something in my htaccess but I've a redirection loop :'(
Here is my .htaccess:
RewriteCond %{QUERY_STRING} ^ID2=([0-9]+)$
RewriteRule message.php? http://website.com/v2/message-%1.php? [L,R=301]
Can someone help me with this?
I suggest not using .php in pretty URL, make it extension-less. Try these rules in v2/.htaccess:
RewriteEngine On
RewriteBase /v2/
RewriteCond %{THE_REQUEST} /message\.php\?ID2=([^&\s]+) [NC]
RewriteRule ^ message-%1? [NE,R=302,L]
RewriteRule ^message-(.+)/?$ message.php?ID2=$1 [L,QSA,NC]

htaccess redirect one folder to another on same domain if request_uri condition false

I'd like to redirect the visitors if they visit the old not working URL example.com/english/* to example.com/german/* but not when they're visiting example.com/
This code in .htaccess is not working:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/german/(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^/english/(.*)$ /german/$1 [R=302,NC,L]
I searched throrough the apache docs and google but nothing does the same.
Where's the problem?
Replace your code with this:
RewriteEngine on
RewriteRule ^english/(.*)$ /german/$1 [R=302,NC,L]
RewriteRule doesn't match leading slash in .htaccess.

.htaccess redirect old html page with space in the name

I've changed an html site to a WordPress installation.
The old .html links have been redirected to the right wordpress page. The old site, which was obviously build by a monkey, had a page /over ons.html. That's right, it had a space in the filename. So now, i'am getting external requests on /over%20ons.html. I can't figure out how i can solve this redirection problem in .htaccess. The page it has to refer to is /over-ons/.
This is my .htaccess so far:
RedirectMatch 301 ^/([^/.]+)\.html$ /$1/
Redirect 301 /over%20ons.html http://www.sterkermerk.nl/over-ons/
Redirect 301 /index.html http://www.sterkermerk.nl/
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The 2nd line contains the rule. I've tried to escape the %20, tried using regex in RedirectMatch, but I can't get it to work.
Anyone got an idea?
Use mod_rewrite instead of mod_alias (Redirect statements) because you're already using mod_rewrite and they'll conflict with each other:
RewriteRule ^over\sons\.html$ http://www.sterkermerk.nl/over-ons/ [L,R=301]
RewriteRule ^index\.html$ http://www.sterkermerk.nl/ [L,R=301]
RewriteRule ^([^/.]+)\.html$ /$1/ [L,R=301]
(replacing the Redirect* statements)
The URI is decoded before being sent through rewrite rules so you don't need to use the %20, just \s or \ will match the space.

htaccess 301 redirect querystring to root

I'm stuck trying to redirect my old posts on my wordpress blog from www.domain.com/?p=444 to just the root, this is because the posts have been removed.
This is what i have tried so far.
RewriteCond %{QUERY_STRING} p=[0-9]+
RewriteRule ^p$ / [L,R=301]
Any help would be much aprreciated.
Best Regards
Use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} p=[0-9]+ [NC]
RewriteRule ^$ /? [L,R=301]
/? means root URL with ? for removing existing query string removed.