htaccess redirect ignored with rewriteengine - apache

I have a htaccess question that I think may be so simple (dumb?) that no one has had to ask it before. I converted from using html pages to php and for the most part the following has worked:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)\.htm$ /htmlrewrite.php?page=$1 [R=301,NC,L]
</IfModule>
There are a couple of exceptions though so I assumed putting:
Redirect 301 /oldpage.htm http://example.com/newpage.php
above the IfMofule... would do the trick but it doesn't. The "Redirect 301" is being ignored unless I remove the rest of the code. Could someone tell me what I'm doing wrong?
TIA.

You're using mod_rewrite and mod_alias together and they are both getting applied to the same URL. You should stick with either mod_rewrite or mod_alias.
mod_rewrite: (remove the Redirect directive)
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^oldpage.htm$ http://example.com/newpage.php [R=301,L]
RewriteRule ^(.+)\.htm$ /htmlrewrite.php?page=$1 [R=301,NC,L]
</IfModule>
mod_alias: (remove everything in the <IfModule mod_rewrite.c> block:
Options +FollowSymlinks
Redirect 301 /oldpage.htm http://example.com/newpage.php
RedirectMatch 301 ^/(.+)\.htm$ /htmlrewrite.php?page=$1

Apache processes mod_rewrite (Rewrite*) first and then mod_alias (Redirect).
Use RewriteCond to prevent /oldpage.htm from processed by mod_rewrite.
RewriteCond %{REQUEST_URI} !^/oldpage\.htm$
RewriteRule ^(.+).htm$ /htmlrewrite.php?page=$1 [R=301,NC,L]
Or use RewriteRule instead of Redirect.
RewriteRule ^oldpage\.htm$ http://example.com/newpage.php [L,R=301]
RewriteRule ^(.+).htm$ /htmlrewrite.php?page=$1 [R=301,NC,L]

Related

I need help placing a 301 redirect in my htaccess file of my new php site

I have a php website and my current .htaccess file is as follows:
<IfModule mod_rewrite.c>
#Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1 [NC,L,QSA]
Options -Indexes
</IfModule>
Now I want to do a 301 redirect on a URL. From example.com/folder/old-url-london.htm to example.com/new-url-london/
The problem is everytime I try something I get:
www.example.com/new-url-london/?page=folder/old-url-london.htm
Now I can't change this line (RewriteRule ^(.*)$ /index.php?page=$1 [NC,L,QSA]) as it is essential to the working of the website.
Any ideas?
I tried the following:
Redirect 301 /folder/old-url-london.htm example.com/new-url-london/
as well as
RewriteRule ^folder/old-url-london.htm$ /new-url-london/ [R=301,L]
Sounds like you are perhaps putting the rule in the wrong place. Or used the Redirect directive. And/or are perhaps now seeing a cached response.
You need to use mod_rewrite RewriteRule to avoid conflicts with the existing rewrite AND the rule needs to be at the top of the file, before the existing rewrite and ideally after the RewriteEngine directive.
For example:
Options +FollowSymlinks -Indexes
RewriteEngine On
RewriteRule ^folder/old-url-london\.htm$ /new-url-london/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?page=$1 [QSA,L]
You will need to clear your browser cache before testing, since any erroneous 301 (permanent) redirects will have been cached by the browser. Test first with 302 (temporary) redirects for this reason.
Don't forget to backslash-escape literal dots in the regex.
I tidied up a few other bits:
<IfModule> wrapper is not required.
Combined Options at the top and re-enabled FollowSymLinks (since it is required by mod_rewrite)
RewriteBase is not required in the directives you've posted.
Removed spurious spacing.
NC flag not required on the rewrite since the pattern .* already matches everything.
The anchors on the pattern ^(.*)$ are not required since regex is greedy by default.

.htaccess rewriterule ends up as 404

I'm trying to do simple redirects in my .htaccess file, but always end up as 404 pages.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^resume$ %{DOCUMENT_ROOT}/Resume.pdf [END,R=301]
RewriteRule ^Resume$ %{DOCUMENT_ROOT}/Resume.pdf [END,R=301]
RewriteRule ^resume.pdf$ %{DOCUMENT_ROOT}/Resume.pdf [END,R=301]
Redirect 301 does not redirect as well.
Try without DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^Resume$ /Resume.pdf [L,R=301]
RewriteRule ^resume(\.pdf)?$ /Resume.pdf [L,R=301]
Because the DOCUMENT_ROOT is added by default, it must not be 2x

.htaccess redirect everything to new domain except one URL to something different

I am 301 redirecting everything from http://something.org/* to https://something.com/* with this code...
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) https://www.something.com/$1 [R=301,L]
</IfModule>
I am trying to add exception that this URL http://something.org/registration/service redirects to http://www.something.com/registration/service (not HTTPS like general rule). Tried this to exclude this url from general rule and it works, but I don't know how to add rule to redirect this exclusion to something else:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/registration/service
RewriteRule (.*) https://www.dewesoft.com/$1 [R=301,L]
</IfModule>
Replace your RewriteCond with this :
RewriteRule ^registration/service http://example.com%{REQUEST_URI} [NE,L,R]

redirectMatch adding a mysterious query string

I've upgraded someone's forum, and there are zillions of URLs that need 301 redirecting to their new homes. For example:
http://www.elfquest.com/social/forum/thread/7851/posters-in-comics/post_572519/
should point to
http://www.elfquest.com/forums/discussion/7851/posters-in-comics/post_572519/
I've figured out a redirectMatch that does the job:
redirectMatch 301 ^/social/forum/thread/(.*) http://www.elfquest.com/forums/discussion/$1
But it adds a curious query string to the end, like so:
http://www.elfquest.com/forums/discussion/7851/posters-in-comics/post_572519/?do=/forum/thread/7851/posters-in-comics/post_572519/
The result is handled without a problem by the new forum software (including the correct canonical reference in the HTML for search engines), but I'd really like to get it "right."
Here's my .htaccess
redirectMatch 301 ^/social/forum/thread/(.*) http://www.elfquest.com/forums/discussion/$1
RewriteOptions inherit
php_value include_path .:/home/elfquestftp/public_html/phpcode
Addhandler application/x-httpd-php .html .htm
#php_flag register_globals on
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
That stuff at the bottom is the standard Wordpress .htaccess stuff. I've also tried this as a rewriterule before the wordpress block ...
RewriteEngine on
RewriteBase /
RewriteRule ^social/forum/thread/(.*)$ http://www.elfquest.com/forums/discussion/$1 [L,R=301]
...and it doesn't seem to do anything.
Don't mix mod_rewrite rules with mod_alias rules. Replace your RedirectMatch with this:
RewriteEngine On
RewriteRule ^social/forum/thread/(.*) http://www.elfquest.com/forums/discussion/$1? [L,R=301,NC]
? at the end of target URL will strip off any existing query string.

How do I rewrite this url with htaccess / mod_rewrite?

how do I rewrite this url with .htaccess mod_rewrite
http://55.100.10.66:81/var/class/tag?isAjax=true&id=189&key=eJwVxzEOwjAMB&callback=_prototypeJSONPCallback_0
to
http://55.100.10.66:81/index.php/var/class/tag?isAjax=true&id=189&key=eJwVxzEOwjAMB&callback=_prototypeJSONPCallback_0
Amongst a lot of things I tried the below which doesn't work
RewriteRule ^var/class/tag(.*) /index.php/var/class/tag$1 [L,NC,QSA]
Thanks!
This is the solution
RewriteCond %{REQUEST_URI} ^/var/class/tag(.*)
RewriteRule .* index.php [L]
You cannot match QUERY_STRING from RewriteRule. 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 /
RewriteRule ^(var/class/tag/?)$ /index.php/$1 [L,NC]