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.
Related
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.
I try to use a simple 301 redirect
from domain1.com/folder/ to domain2.com/
but excluding domain1.com/folder/subfolder
I use the following code in .htaccess:
RedirectMatch 301 ^/folder/((?!subfolder).*)$ https://domain2.com/$1
but it simply redirects all the requests, including the requests to subfolder.
Please, help to fix the line to make it work as described. Thank you!
here is the complete code of .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]
</IfModule>
RedirectMatch 301 ^/folder/((?!subfolder).*)$ https://domain2.com/$1
Try it like this using mod_rewrite instead:
(NB: This assumes the .htaccess file is located in the document root.)
# /.htaccess
# Redirect all direct requests, except "subfolder"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond $1 !^subfolder($|/)
RewriteRule ^folder/(.*) https://domain2.com/$1 [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]
</IfModule>
It is important that the redirect goes before the rewrite to your front-controller.
You will need to ensure your browser cache is cleared before testing and test with a 302 (temporary) redirect to avoid potential caching issues.
UPDATE:
Yes, /folder has it's own .htaccess (this is the file I am working at all this time). Yes, /folder is where Wordpress is installed.
In that case you would need to change the above redirect to read as follows (it won't do anything otherwise):
# /folder/.htaccess
# Redirect all direct requests, except "subfolder"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond $1 !^subfolder($|/)
RewriteRule (.*) https://domain2.com/$1 [R=301,L]
Basically, you need to remove folder/ from the start of the regex that matches the URL-path. The URL-path that the RewriteRule pattern matches against is relative to the directory that contains the .htaccess file.
The addition of the check against the REDIRECT_STATUS env var is to ensure that rewritten requests to the WP front-controller (when subfolder is requested) are not redirected.
You can also "simplify" the WordPress directives that follow (although if these are enclosed in # BEGIN WordPress / # END WordPress comment markers then you should leave the directives as they are since they are maintained by WordPress). For example:
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
The RewriteBase directive is not required. And neither is the <IfModule> wrapper. (But as I said above, only change this if you are hand-coding the .htaccess and not letting WordPress maintain it.)
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]
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]
I have a RedirectMatch rule in my .htaccess file that works fine, but I'm having trouble coming up with a comparable rule using mod_rewrite.
My goal is to have this URL: mysite.com/anything/print show this page: mysite.com/anything?view=print.
The rule that works to Redirect it is this:
RedirectMatch 301 ^(.*)/print/?$ http://mysite.com/$1?view=print
But now I'd like to change this from a visible 301 redirect to an "invisible" rewrite using mod_rewrite. I have tried many different variations on this (with and without RewriteBase), and none have worked:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/print/? $1?view=print
What am I doing wrong? Mod_rewrite is definitely enabled, and there are functioning Wordpress-based mod_rewrite rules in the same .htaccess file.
UPDATE
Using tips from #Nathan, I now have this. However, I still get a 404 when I visit mypost/print.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)/print/?$ /index.php/$1?view=print [L]
When I append /print/ to a permalink, the WP_Debug plugin indicates the following:
Request: myposttype/mypost/print
Query String: attachment=print
Matched Rewrite Rule: myposttype/[^/]+/([^/]+)/?$
Matched Rewrite Query: attachment=print
If you have typical wordpress rules in the htaccess file, your rules should come before the # BEGIN WordPress block. Otherwise the wordpress rules will stop the rewrite matching before your rules get called.
Also, you should add $ after your regex pattern unless you also want to match something like: http://domain.com/page/print/something/else/here
Lastly, in order for Wordpress to recognize the change in the URL without redirecting the page, you need to append the URL to index.php/ so that Wordpress will use path info permalinks.
E.g.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)/print/?$ /index.php/$1?view=print [L]
# 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
RewriteEngine on
RewriteRule ^(.*)/print/$ /$1?view=print [L]
or
RewriteRule ^(.*)/print$ /$1?view=print [L]
without "/" in the end of URL string