.htaccess redirect problems - .htaccess

I'm using WordPress on my domain. I originally installed it in a sub-directory:
www.example.com/wpblog/
I have now moved the blog into the root of my site AND I have changed the permalink structure from ../2011/03/name-of-post to ../name-of-post.
I am trying to redirect all links to my blog which use the old URL and structure.
I use this code to successfully redirect from /wpblog/ to the site root:
RewriteEngine On
RewriteBase /
Redirect 301 /wpblog/ http://www.example.com/
But now I need to change the OLD permalink structure to remove the /2011/03/, leaving me with www.example.com/name-of-post (from: www.example.com/wpblog/2011/03/name-of-post). I added this code, which works 100%:
RedirectMatch 301 /([0-9]+)/([0-9]+)/(.*)$ http://www.example.com/$3
The trouble is, that also breaks links to my media files (www.example.com/wp-content/2011/03/name-of-media). I therefore need to exclude the /wp-content/ directory from the permalink redirect (but not the /wpblog/ redirect). I changed the permalink redirect thus:
RewriteCond %{REQUEST_URI} "/wp-content/"
RewriteRule 301 /([0-9]+)/([0-9]+)/(.*)$ http://www.example.com/$3
But this breaks the entire site, giving me an error.....
I'd be really grateful if someone could help me out! I've been tearing my hair out over this!

Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} ^/wp-content/ [NC]
RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule %{REQUEST_FILENAME} !-l
RewriteRule /(?:[\d]+)/(?:[\d]+)/(.*)$ http://www.grvhi.com/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Related

htaccess url rewriting from request query to folder like and changing the php name

How can I do the following on the localhost which will be hosted later online:
I have this link:
http://localhost/shops/shop.php?c=15
I want to rewrite it to http://localhost/shops/shop/15
htaccess is(which is placed in C:\wamp\www\shops folder):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?RECIPES/(.*?)/?$ /single-product-details.php?=$1 [L]
RewriteRule ^/?SHOP/(.*?)/?$ /shop.php?=$1 [L]
With your shown samples, please try following Rules. Place your htaccess file along with shops folder(not inside it).
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /shops/
##External redirect to friendly url.
RewriteCond %{THE_REQUEST} \s/shops/(shop)\.php\?c=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Internal rewrite to actual url/files.
RewriteRule ^([^/]*)/(.*)/? $1.php?c=$2 [QSA,L]

Drupal: Correcting URL link access for subdirectory site Installation

I've installed Drupal 7 to a sub-directory on my server and used .htaccess mod_rewrites to fix most of the URL pathing issues, I'm now looking for a way to strip the sub-directory from the URL when it's explicitly requested.
To be clear, the site can be accessed from 'example.com' and the internal links work fine, but I'm looking to prevent requests to 'example.com/sub/file' from working, Ideally with a 301 back to 'example.com/file'.
My .htaccess at the moment looks like this:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ sub/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/sub%{REQUEST_URI} -f
RewriteRule .* sub/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* sub/index.php?Q$0 [QSA]
Have you tried adding:
Redirect 301 /\/sub/file http://www.example.com/file
to your .htaccess?

RewriteRule works in .htaccess not in another included file?

I have a server with Magento and Wordpress.
Magento is in the website root /, Wordpress under /blog/.
Various Redirect 301 and RedirectMatch permanent are specified in a file in the website root. This file is "included" from the Apache vhost configuration:
Include /var/www/html/myredirects
Some pages from Wordpress should be visible from the website root, so that
http://www.mymagento.com/pretty-url/
will show the same content as
http://www.mymagento.com/blog/pretty-url/
This is currently achieved using in the .htaccess file using some rewrites as follows:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^pretty-url/ /blog/pretty-url/ [L]
[...more like the above here and the common Magento rewrites follow...]
RewriteRule ^api/rest api.php?type=rest [QSA,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^index.php
RewriteCond %{REQUEST_URI} !\.(html|jpg|png|gif)$
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
RewriteRule ^(.*)$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
Is it possible to move just the RewriteRule lines that show some pages from the blog in the root to the myredirects file?
I tried moving them with and without the part
Options +FollowSymLinks
RewriteEngine on
and in both cases it does not work, either a blank page or a Server Error is the result.
I cannot find in the Apache documentation what is the reason why whatever is defined in the .htaccess makes it work. Maybe it's because of the processing order?
Edit, as asked how myredirects looks like (please note these redirects work already):
They are a bunch of URLs with different product categories from Magento plus some related to the /blog/, they all look like these:
# These are Magento
RedirectMatch permanent /product-category/sub-category-1/sub-category-2/(colorA|colorB|colorC) /sub-category-2/$1
Redirect 301 /someurl/otherurl/ /another/url
# These are for Wordpress
Redirect 301 /blog/tag/some-tag/ /blog/some-tag/
Redirect 301 /blog/tag/another-tag/ /blog/another-tag/
Those for the blog are only redirecting the /tag/ part and nothing else.
And yes, it's only some of the /blog/ URLs that should be shown also in the website root, others will stay under /blog/ (I know there will be duplicate content for those shown in both paths).

HTACCESS redirect all old domain urls to new domain

I am new to HTACCESS and have a very specific question regarding 301 redirects. I am looking to redirect all .co.uk urls to .co but also I want to redirect all suburls to the root URL so for example I want
http://virtualfiles.co.uk/scenarios.html -> http://virtualfiles.co
A lot of these pages don't exist anymore so I would rather just relocate the user to the top level domain rather than a page that doesn't exist on the old or new domain.
I am set up on WordPress and the code I have so far is below. Any help would be amazing.
# 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
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [NC]
You can try this in your domain1/.htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?virtualfiles\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://virtualfiles.co/$1 [R=302,L]
Or Try With RedirectMatch
RedirectMatch 301 ^/(.*)$ http://virtualfiles.co/$1

Rewrite Rule custom index file Drupal installation

This is the current Rewrite rule of Drupal set in my htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{REQUEST_URI} !=/index.html
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This works well, but the problem is that I want to have a custom page which will open when people browse to the website. More specific:
www.company.com/ should open index.html
All other pages should use the default rewriterule of Drupal.
I tried editing the DirectoryIndex, but then all pages are redirected to index.html.
DirectoryIndex index.html index.php
I also tried adding different Rewrite Rules but I didn't have any luck yet. Htaccess is not my best skill.
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^/$ /index.html [L]
Probably there will be an easy solution, but I can't figure out what to do.
Does anyone know how to solve my problem?
Thanks!
Try adding to the rule the 301 code, meaning permanently moved.
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^/$ /index.html [L,R=301]

Resources