redirectMatch 301 regex is not working - .htaccess

I want to redirect http://site.com/home?page=123 http://site.com/home
but the following rule doesnt work
redirectMatch 301 ^/home/\?(.*)$ http://www.site.com/
Any help would be appreciated. Thanks

Unfortunately RedirectMatch directive does not work with query string -- only with path part of the URL. You have to use mod_rewrite for that:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} =page=123
RewriteRule ^home$ http://www.site.com/? [R=301,L]
Place it in .htaccess in website root folder. If placed elsewhere some tweaking may be required.
It will ONLY redirect request for /home?page=123. All other requests (e.g. /home?page=123&extra=hello) will be ignored.

Related

.htaccess Redirect 301 with Rewriterule not working

When I try to redirect my page, nothing happens. If anyone tries to enter forum.example.com/forumdisplay.php?fid=1 I want them to be redirected to forum.example.com/forum-1.php. I am new to .htaccess and couldn't figure it out.
.htaccess file:
Options -MultiViews
RewriteEngine on
Redirect 301 /forumdisplay.php?fid=([0-9]+).php /forum-$1.php
RewriteRule ^forum-([0-9]+)\.php$ forumdisplay.php?fid=$1 [L,QSA]
Any help will be appreciated.
Redirect (mod_alias) and RewriteRule (mod_rewrite) belong to two different modules. You need to use mod_rewrite only for this. Try something like the following:
Options -MultiViews
RewriteEngine on
# Redirect direct requests for the "real" URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^fid=([0-9]+)
RewriteRule ^forumdisplay\.php$ /forum-%1.php? [R=301,L]
# Internally rewrite back to the "real" URL
RewriteRule ^forum-([0-9]+)\.php$ forumdisplay.php?fid=$1 [L,QSA]
(Redirect doesn't use regex either.)
UPDATE: I've appended a ? onto the end of the first RewriteRule substitution (ie. /forum-%1.php?). By specifying an empty query string, it removes the query string from the request. Alternatively, you can use the QSD flag on Apache 2.4+

.htaccess; 301 redirect not working

I tried the following code in .htaccess to 301 redirect www.example.com/?content=file.php&id=16 www.example.com/file/This-is-the-title/16
RewriteEngine on
Redirect 301 /?content=file.php&id=16 /file/This-is-the-title/16
But it's not redirecting. The URL remains as it is.
What am I doing wrong?
P.S. I'm not asking for rewrite or so. I need a 301 redirect.
The Redirect directive doesn't match query strings. Use this instead:
RewriteEngine on
RewriteCond %{QUERY_STRING} =content=file.php&id=16
RewriteRule ^$ /file/This-is-the-title/16? [R=301,L]

How to make htaccess rule for a URL with specific characters?

I need to 301 redirect all ?print=yes URLs to the URLs without ?print=yes that contain the same name in them through .htaccess. Currently the button to PRINT is present in the header of the website so it's more than 70 URLs to fix... Removing the button to PRINT the page will ruin the design quite a bit, so it's not really an option.
I think it needs to be a RedirectMatch rule, but how do I write it?
Example: redirect 301 from domain.com/faq/?print=yes to domain.com/faq
Thanks in advance!
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^print=yes$ [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L,NE]
I am not sure if redirect can do the same but here is how I would do it with rewrite
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\?print=yes$ $1 [NC]

Redirect all the old url to new using htaccess

I had an old site implemented on liferay. All the links were like
www.site.com/web/something
I want to write a htaccess rule so that all the requestes with /web/* should
be redirected to the homepage permanenty...
Now for individual url i have wrote this.
Redirect 301 /web/contact http://www.site.com/contact
Is there a wildcard method or something for this?
How about this which redirects /web/something to http://site.com/something:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^web/(.*) http://site.com/$1 [R=301,L]
</IfModule>
Or anything anything starting with /web/ redirects to homepage:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^web/.* http://site.com/ [R=301,L]
</IfModule>
Alternatively, staying within mod_alias, you can just use RedirectMatch to use a wildcard:
RedirectMatch 301 /web/(.*) http://www.site.com/$1

htaccess 301 redirect folder, but not subfolders

I need to redirect from:
http://example.com/folder
to http://example.com/newfolder
But leave:
http://example.com/folder/subfolder
Where it is. Is this possible? I can't seem to make it happen without causing a heap of redirect chaos.
Jestep's answer above redirects "/root-directory" but fails to redirect "/root-directory/". This can be fixed and simplified by simply:
RewriteCond %{REQUEST_URI} ^/folder/?$
RewriteRule (.*) /newfolder [R=301,L]
This will redirect "/folder" and "/folder/" but leave all sub-directories alone.
I've tried other methods here with mixed success. I am no Apache expert by any means, but here is the simple one-liner that works every time for me. I just use RedirectMatch instead of Redirect and include a very simple RegEx to end the match at or just before the trailing slash, meaning that subdirectories should never qualify as a match.
RedirectMatch 301 ^/folder[/]?$ /newfolder
I just ran into this and here's the solution I came up with. I prefer this method because it doesn't redirect any subdirectory.
RewriteCond %{REQUEST_URI} ^/root-directory[/]?
RewriteCond %{REQUEST_URI} !^/root-directory/+[/]?
RewriteRule (.*) http://www.example.com/ [R=301,L]
Maybe:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder[/]?
RewriteCond %{REQUEST_URI} !^/folder/subfolder[/]?
RewriteRule (.*) /newfolder/$1 [R=301,L]
This should redirect /folder to /newfolder but leave out /folder/subfolder
If mod-rewrite isn't enabled or you are unable to use RewriteRule directive on your server, you can use RedirectMatch directive of mod-alias which is the default module of apache httpd server.
RedirectMatch 301 ^/folder/?$ http://example.com/newfolder/
This will 301 redirect /folder/ to /newfolder/ .
Which server are you using?
You could for example use mod_rewrite if you use apache and do something like this
RewriteEngine On
RewriteOptions Inherit
RedirectMatch permanent ^/folder/$ http://example.com/newfolder
#I haven't tested the above redirect btw ^
and put that in a .htaccess file in your /folder/ directory (assuming you can alter apache's settings, meaning you have the option AllowOverride All in that virtual host)
Here's some more info http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Resources