Switching from ExpressionEngine to Wordpress and need to set up redirects in htaccess.
The paths to the articles will change from mysite.com/section/read/article-name
to mysite.com/article-name/. The section part of the path has six variants.
Unsure whether to look at redirectmatch or rewrite rule being a toatl novice with htaccess.
Thanks
Either would work, so I'll use RedirectMatch. To be specific about the section name, it would be:
RedirectMatch 301 "/(?:section|another-section|third-section)/read/(.+)$" /$1
Replacing your section names in there, separated by pipes.
Or for anything in a directory called read in a top level directory:
RedirectMatch 301 "/[^/]+/read/(.+)$" /$1
Since it is a one to one mapping and just the final part to be preserved, you don't need RedirectMatch. Redirect is sufficient
Redirect /section/read /
When it works as expected, you may set the status code to 301.
Redirect 301 /section/read /
If you want to use mod_rewrite instead, this would be
RewriteRule ^section/read/(.*)$ /$1 [R,L]
When everything works as it should, you may replace R with R=301. Never test with R=301.
Related
I am cleaning up some old stuff and I need to do some 301 redirects.
For example:
I have following absolute url: http://mysite.dk/product.asp?product=371&sub=0&page=1
That i need to redirect to:
http://mysite.dk/category/test
I have tried with htaccess:
RedirectMatch 301 /product.asp?product=371&sub=0&page=1 /category/test
But above code will not work, It is showing the 404 page and ignoring the redirect I just made. Note please that product.asp does not exists as a file!
However if I use:
RedirectMatch 301 /product.asp /category/test/
It works sort off, but now I can't do redirects, based on the query string for that specific file.
Any suggestions? Btw I am using Prestashop=)
Best, Simon
You cannot match query string using RedirectMatch directive. Use mod_rewrite rule instead:
RewriteEngine On
RewriteCond %{QUERY_STRING} product=371&sub=0&page=1
RewriteRule ^product\.asp$ /category/test/? [L,NC,R=301]
? in the target is to strip off existing query string.
mod_alias is designed to handle simple URL manipulation tasks. For more complicated tasks such as manipulating the query string, use the tools provided by mod_rewrite.
from here
Just add the rules from #anubhava answer into Prestashop .htaccess and it will works. Better to do it before # ~~start~~ line, you will see notice in the file.
What I'm trying to do is redirect example.com/author/authorname/ to example.com/author/authorname/page/1/ for the pagination to work more efficiently at the initial load of a page.
Currently I got
RedirectMatch /example/author/(.*) http://localhost:8888/example/author/(.*)/page/1/
I was trying to use a wildcard variable but that doesn't seem to work. In the first place where the (.*) is I would need to store that value as a variable and use that exact same name in the similar place in the redirection location.
Try:
RedirectMatch ^/example/author/([^/]+)/?$ /example/author/$1/page/1
or if you already have mod_rewrite rules, you should probably stick to that:
RewriteEngine On
RewriteRule ^example/author/([^/]+)/?$ /example/author/$1/page/1 [L,R]
I bought an old site that apparently used some kind of a CMS, maybe wordpress.
Now it's a simple html site. I want to redirect www.example.com/main and everything in that directory to root. In other words, if a url is example.com/main/a_bunch_of_garbage that should 301 redirect to the homepage. Here is what I have but it doesn't quite work.
Options +FollowSymLinks
RewriteEngine on
RedirectMatch 301 ^/main/(.*)$ http://www.example.com
If the url doesn't contain any weird characters like ? or = then it works. But for example, if I go to www.example.com/main/index.php?option=com_content&view=post&id=84&Itemid=982/
Then although it does redirect to the homepage, in the address bar, I still see the entire URL. Can I fix this?
I also just remembered another problem I had. If I put example.com/main (without forward slash after main) the redirect doesn't work. It only works if I put example.com/main/ (with forward slash)
You need to use mod_rewrite rules only and avoid mixing RedirectMatch here. Have your rules like this in your root .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^main(/.*)$ $1? [L,NC,R=301]
? in the end is used to strip off any existing query string.
i add this rules to my .htaccess file:
RedirectMatch 301 /wp-content/my-image-5x5.jpg /wp-content/default-5x5.jpg
Redirect works well, but add the old image name into address.
Why? I don't need this.
What i'm get now:
www.mypage.lt/wp-content/default-5x5.jpg?file=/wp-content/my-image-5x5.jpg
What i need:
www.mypage.lt/wp-content/default-5x5.jpg
Redirect works well, but add the old image name into address. Why? I don't need this.
This is because you have wordpress rules which does internal routing (to an index.php file) and the rewrite rules belong to mod_rewrite, while the RedirectMatch directive belongs to mod_alias. These modules both get applied at different points in the URL-file mapping pipeline, thus both get applied, and you end up with a mangled redirect URL. You should stick to only mod_rewrite in this instance. Try adding these rules before your wordpress rules:
RewriteEngine On
RewriteRule ^/?wp-content/my-image-5x5.jpg /wp-content/default-5x5.jpg [L,R=301]
Sorry for the long title. I'm currently writing some 301 redirects and using mod rewrite (Apache) to handle them. Currently only one of the two 301s I have tried is working. Here is the code:
#301 REDIRECTS
RewriteEngine On
RewriteRule ^Fox-and-Frank-home\.html$ http://www.mydomain.co.uk/contact_us.php [R=301]
RewriteRule ^about\.html$ http://www.domain.com/about/ [R=301,L]
about.html properly redirects, but Fox-and-Frank-home.html does not. I have tried this with other names, other URLs, but it is not working. Any help will be greatly appreciated.
EDIT
I have gotten this to work on a completely bare .htaccess file. Do 301 redirects need to be at the very top before everything else?
You don't have a L (last) flag on your Fox-and-Frank rewrite, so other rules can potentially be processed, and definetly will be in a .htaccess (where you need to use the END flag).
Try changing it to:
RewriteRule ^Fox-and-Frank-home\.html$ http://www.mydomain.co.uk/contact-us.php [NC,R=301,L]
Where:
NC makes it a case insensitive check (so fox-and-frank-home would
redirect to)
R=301 is the 301 redirect (although you can just put R
and 301 would be assumed I always specify it personally)
L tells
mod_rewrite to stop processing further rules (but as its in a
.htaccess. you may need to use END instead - see http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l).
Regards.