I need to serve these pages:
/category/*
As
/shop/category/*
I need the url to have /shop in front of /category/ and it needs to handle all nested path structures within category also.
How can I do this via .htaccess preferred?
There is also a /shop/ page that I do not want disturbed by any .htaccess edits.
So, I just need to serve /category/ pages with the url having /shop/category/ instead, is this possible via .htaccess?
So I'm a beginner here in .htaccess rewrites, but have tried this:
RewriteCond %{HTTP_HOST} ^category/$
RewriteRule ^shop/category/(.*)$ category/$1 [L]
It doesn't do anything tho...
Also tried this:
RewriteCond %{HTTP_HOST} ^category/(.*)
RewriteRule ^$ /shop/category/$1? [R,L]
Again, no effect on anything.
I am using Wordpress, so it seems it has some power over my rewrite rule perhaps?? Anyways, here is my entire .htaccess file.
# 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
RewriteRule ^shop/category/(.*)$ category/$1 [R,L]
Have tried the following within the init wordpress action also:
add_rewrite_rule('^/shop/category(?:/.*)?', '/category/$matches[1]', 'top');
No luck on this.
Ok, seems that I might be getting somewhere here...
The following add_rewrite_rule below matches only the first category, but doesn't match for child categories:
add_rewrite_rule('shop/category/?([^/]*)', 'index.php?product_category=$matches[1]', 'top');
The custom taxonomy that I am using is product_category, not wordpress default category since I don't want my product categories mixed with post categories. So, I just need to figure out now, how to match all paths (which include child categories), for example:
/category/gelest-inc/silanes-silicones/
silanes-silicones is a child of gelest-inc, how to capture this in the rewrite rule above also?
If I understand your question correctly following .htaccess should serve your requirements:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^category(?:/.*)?$ /shop/$0 [R=301,NC,NE,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Your rule says if HTTP_HOST is catagory/ then rewritehomepage (^$ matches an empty uri eg : /) to /shop/catagory. Your RewriteCond never meets as you are matching against HTTP_HOST header using URI in pattern.
If you want to redirect /catagory/ to /shop/catagory , you can use
RewriteEngine on
RewriteRule ^catagory/([^/]*)/?$ /shop/catagory/$1 [L,R]
You have the WordPress rules, and after the rules comes the "shop" rule.
When the shop URI is not a real file or directory, requests for it will already be handled by the WordPress rules and rewritten to index.php. Apache then iterates a second time with index.php, but it doesn't match the shop rule and never redirects or rewrites.
To make this work the shop rule must come first
RewriteRule ^shop/category/(.*)$ category/$1 [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Now it depends on what you want to achieve, redirect from
RewriteRule ^shop/category/(.*)$ category/$1 [R,L]
or to shop/category
RewriteRule ^category/(.*)$ shop/category/$1 [R,L]
Related
I should make some url redirections (no variables to pass) in .htaccess but I don't want to let see the destination url. How can I make?
I've already tried to use Redirection instruction like this:
Redirect /en/folder1/url1 /en/folder2/url2
But in this way the destination url is visible. So I started to try to use RewriteRule like in this example:
RewriteRule ^/en/folder1/url1$ /en/folder2/url2
but I've always error page as result.
P.S. This code is inside a .htaccess file with the default Wordpress url rewrite code.
EDIT. This is the complete .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^en/folder1/url1$ en/folder2/url2
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You need to remove the leading slash from your Rule's pattern
RewriteEngine on
RewriteRule ^en/folder1/url1$ /en/folder2/url2 [L]
Greetings and thank you in advance for your help!
I am trying to redirect certain subfolders from the "category" subfolder, but not all of them. Only about 5 of them.
Example:
http://domain.com/category/fish
http://domain.com/category/lamb
Should redirect to
http://domain.com/tasty/fish
http://domain.com/tasty/lamb
But:
http://domain.com/category/lead-paint
Should not.
What I've tried (in .htaccess):
RewriteRule ^/category/fish http://example.com/tasty/fish [R=301,L]
RewriteRule ^/category/lamb http://example.com/tasty/lamb [R=301,L]
I tried both before and after the Wordpress module. I also tried inserting inside the Wordpress module like so:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/category/fish http://example.com/tasty/fish [R=301,L]
RewriteRule ^/category/lamb http://example.com/tasty/lamb [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Changes to .htaccess definately work. Typos generate a 500 response, and I can block by IP address. I'm not sure if Wordpress is overriding my redirects, or if I have a syntax error.
Thank you again.
You need to remove the leading slash just after ^ in the two rules. You also don't need to specify the full domain. The following will do
RewriteRule ^category/fish/?$ /tasty/fish [R=302,L]
(Note the 302 - always test with a temporary redirect, then change to permanent when you're happy.)
I have two domain names setup with sub-domains as follows...
blog.domain.com
www.blog.domain.com
blog.domain.info
www.blog.domain.info
Both domains are pointing to the same location on the same server, a directory containing WordPress. (domain.com/blog)
To keep Google happy, I want everything to redirect to this one domain...
blog.domain.com
Here is what's inside the .htaccess file contained in the WordPress directory...
# 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
So I went into my cPanel and added a 301 Domain Redirect as follows...
blog.domain.info -> blog.domain.com (with "www" optional and wildcard selected).
cPanel then automatically added the following to the same .htaccess file under the WordPress rewrite rules...
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com$1" [R=301,L]
The problem is that the wildcard portion does not seem to work.
When I go to blog.domain.info, I get redirected to blog.domain.com as expected.
But when I go to blog.domain.info/my-post, I do not get redirected at all.
How can I fix this? I've tried rewrite rules that I know work but all I can think of is that the WordPress rules are interfering.
Once it's fixed, can I move these mod-rewrites to the main .htaccess in the hosting account's www root keeping them separate from the WordPress rules? Edit: Answer- NO, they will not work because they are domains parked in directories off the root www.
Thank-you!
Looks like I simply had to move the new rules above the WordPress section. I also added a new one that is supposed to remove the 'www' from the dot com domain name.
This all seems to be working.
Any comments appreciated.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.com$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com\/$1" [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
EDIT
It should be noted that if you change anything in the htaccess file contained between these Wordpress comments, it might get overwritten by Wordpress at a later time. Moving your custom edits outside AND above this block also works and is immune from any changes to htaccess made by Wordpress itself.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.com$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com\/$1" [R=301,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
i own multiple domains for my site MethodShop, including methodshop.com, methodshop.net, etc.. When doing maintenance, i'll divert traffic between the different sites so my users don't have their experience interrupted. below is the htaccess wildcard script i use. it takes whatever URL the user attempts to access and mirrors that link on another domain.
for example,
http://methodshop.NET/games/play/bubblewrap/index.shtml
would redirect to
http://methodshop.COM/games/play/bubblewrap/index.shtml
here's the htaccess script for methodshop.net that rewrites all methodshop.net URLs to methodshop.com. just edit it for your domain.
RewriteEngine on
RewriteRule (.*)$ http://www.methodshop.com\/$1 [R=301,L]
So, I'm trying to add my own RewriteRule in addition to what Wordpress already has. A little backgroun. The site I am working on is built off of a custom CMS, but also has a blog that is powered by WordPress. So, heres what I have thus far:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Custom rewrite for locatons
RewriteRule ^locations/(.*)$ /locations2.php?info=$1
# Wordpress rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
I also got rid of a couple lines of the Wordpress Rewrite code that seemed unecessary (everything still works....I think). But yeah, it seems like it is overwriting the rule that I wrote. Help?
Oh, also, I just tried to get rid of the . that is before /blog/index.php [L] and replaced it with just a ^, it worked...kind of. It loaded the blog when I went to /blog/, but when I went to something like /blog/author/admin, I got a 404.
I think you need to add the last flag [L] after your new rule, otherwise processing will continue and your request will be rewritten to '/blog/index.php'.
RewriteRule ^locations/(.*)$ /locations2.php?info=$1 [L]
Have you tried to remove the RewriteBase / and the slash before locations2.php ?
Something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
# Custom rewrite for locatons
RewriteRule ^locations/(.*)$ locations2.php?info=$1
# Wordpress rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
If the blog gets unreacheable, move RewriteBase / to after the custom rewrite. Also, I believe that if the blog is in a sub-directory, the RewriteBase should be RewriteBase /blog/
I'm trying to combine the following code so that the WordPress permalinks work in the main directory, waringis.com (top code) and a second domain, burrowpress.com, is redirected to the subdirectory 'waringis.com/burrowpress' (bottom code) -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} burrowpress.com$ [NC]
RewriteCond %{REQUEST_URI} !^/burrowpress/.*$
RewriteRule ^(.*)$ /burrowpress/$1
Right now permalinks are working in WordPress and the redirect works but images have to be direct links to waringis.com/burrowpress/images/ instead of burrowpress.com/images/ - Any help is much appreciated...
You need to swap your code blocks around. The [L] flag on the WordPress rules are stopping execution of the file at that line in their code since your special path would "pass" the WordPress REWRITE_COND statements:
RewriteEngine On
RewriteCond %{HTTP_HOST} burrowpress.com$ [NC]
RewriteCond %{REQUEST_URI} !^/burrowpress/.*$
RewriteRule ^(.*)$ /burrowpress/$1 [L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You don't need two RewriteEngine On statements, but since WordPress is able to rewrite your .htaccess file (depending on how you have it setup) you might want to leave it. If you are updating your file manually, you can remove the second RewriteEngine on directive.
The important part is that I moved your special rules ahead of wordpress.
I'm not a .htaccess expert but i would say that you don't need to include RewriteEngine On twice that you can put the redirect code in the if statement. So you want to redirect to the subdomain but not have your images be kept in the subdomain is this correct?