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).
Related
I am currently building a PHP-based portfolio site without any frameworks whatsoever. I have created an index.php file in the root and a lot of folders namely /about, /contact, /portfolio and the like. Within each of those, I have a separate index.php file
I created a .htaccess file and in it, I have this code...
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
For some reason when I visit my site at example.com/index.php or example.com/about/index.php the .htaccess file is not working and removing it.
Any ideas why?
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
If the intention with these directives is to remove index.php from the requested URL then you are using the wrong rules! This rule would route a request for /something to index.php/something (passing the requested URL-path as path-info to index.php) providing /something does not map to a file or directory.
You have presumably structured your URLs so they map to filesystem directories from which the DirectoryIndex is served, so the above directives would seem to be entirely redundant. (?)
To remove index.php from the end of any URL
To remove index.php (the DirectoryIndex) from any URL you would need to do something like the following instead:
RewriteRule ^(.+/)?index\.php$ /$1 [R=301,L]
Test first with a 302 (temporary) redirect to avoid potential caching issues.
To clarify...
you must not be linking internally to /index.php or /about/index.php. The above directive is only for when a user or external site erroneously requests the index.php file directly.
And include trailing slashes on your internal links. eg. you should be internally linking to /about/ and /contact/ etc. (not /about or /contact), otherwise mod_dir will implicitly issue a 301 redirect to append the trailing slash.
It took a while and thanks for the suggestions by Mr White this is my solution..
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
Works perfectly now.
Thank you for everyone who took the time out to help me on this.
In the root folder of my hosting I have an htaccess file with, among other things, the following code which redirect to https and www.:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
In the /news/ folder i have entry.php file which have for example ?slug=this-is-first-entry. I want it to look like this https://www.example.com/news/this-is-first-entry.
So I want redirect this https://www.example.com/news/entry.php?slug=this-is-first-entry to this https://www.example.com/news/this-is-first-entry
I have this code in .htaccess in /news/ folder:
RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]
It's working fine, but redirecting https and www from root folder does not work. Please help, I am not familiar with htaccess.
This is a THUMB rule. If current directory has a .htaccess with RewriteEngine ON then it overrides parent .htaccess directives. If you want parent .htaccess rules to be applied before then use following option:
RewriteOptions InheritBefore
Before RewriteEngine On line.
So your htaccess file inside /news/ folder will become like:
RewriteOptions InheritBefore
RewriteEngine ON
RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]
Additional suggestion: In case you are trying to rewrite non existing files and directories if this is the case then have your htaccess in your news folder like as follows.
RewriteOptions InheritBefore
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]
Here is our current .htaccess file with the rules we need to keep but also we need to add a new rule that redirects from the root domain to a subfolder URL
example.com -> example.com/fl/en.html..
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.nz$
RewriteCond %{REQUEST_URI} !^/subfolder [NC]
RewriteRule ^(.*)$ /subfolder/$1 [L]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(/home.html|/info.html|/flash|/external)
RewriteRule (.*) http://example.com/fl/en.html [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
There doesn't appear to be anything particularly special required, providing you put the directive in the correct place. To redirect the document root (for example.com) to /fl/en.html in .htaccess can be done like so:
RewriteRule ^$ /fl/en.html [R,L]
This just needs to go after the directive that rewrites everything for the host domain.co.nz and before your front-controller. (You could potentially combine this with your existing directive that redirects /home.html, /info.html, etc.)
Your existing rules could be further optimised. Instead of checking the requested URL-path in a RewriteCond directive and allowing everything through in the RewriteRule pattern, it is more efficient to do what you can in the RewriteRule pattern first (since this is what is processed first).
Also, since you are using WordPress, any custom directives you add to .htaccess should be outside of the # BEGIN WordPress section. WordPress itself maintains this section, so any manual customisations you make could be overwritten during an update.
Also, there is no need to repeat the RewriteEngine directive. (The last instance of this directive wins and controls the entire file.)
So, bringing all this together, we have something like:
# Rewrite all requests for domain.co.nz to subfolder
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.nz$
RewriteRule !^subfolder /subfolder%{REQUEST_URI} [L]
# Redirect document root to /fl/en.html
RewriteRule ^$ /fl/en.html [R,L]
# Redirect specific paths to /fl/en.html
RewriteRule ^(/home.html|/info.html|/flash|/external) /fl/en.html [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
Note that the above directive (with a single R) is a temporary (302) redirect. Change this to R=301 if this is intended to be permanent, but only once you have tested that it's working OK (to avoid caching issues).
As always, make sure you've cleared your browser cache before testing.
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]
In my website root I have the following to redirect to non www domain
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^www\.notails\.com [NC]
RewriteRule ^(.*)$ http://notails.com/$1 [R=301,NC]
in a subfolder named 'photography' I have this...
RewriteEngine on
RewriteRule ^show/([^/\.]+)/([^/]+)$ show.php?section=$1&photoid=$2 [L]
Anything inside the photography folder ignores the www removing rule. How do I get these two rules to both apply to folders/files within the photography folder?
Also... my root htaccess file has this...
# 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
Could it be interfering? I have a self-hosted wordpress blog but it's not in the root of the website, it's in a subfolder called 'blog' so I don't know why this rule is in my root's htaccess file. Can/should I move it?
Edit: Just to point out, in case it isn't obvious - I'm a complete noob when it comes to htaccess and mod_rewrite stuff. Does a htaccess file in a subfolder override any htaccess files nearer to the root than it? Or do the htaccess contents combine?
Edit 2: I have tried moving the second rule to the same htaccess file as the www removing rule as per the following code...
# 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
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^www\.notails\.com [NC]
RewriteRule ^(.*)$ http://notails.com/$1 [R=301,NC]
RewriteRule ^photography/show/([^/\.]+)/([^/]+)$ photography/show.php?section=$1&photoid=$2 [L]
If I then go to one of my photography pages it resolves to the intended url (http://notails.com/photography/show/pointofayre/260 for example) but the page is a 404.
If I manually add 'www' to that it undoes the other rule... (http://notails.com/show.php?section=pointofayre&photoid=260) and removes 'photography/' from it.
Add RewriteOptions inherit into .htaccess in your "photography" folder because right now all rewrite rules from parent folders are ignored (default behaviour).
Alternatively move rewrite rules from that subfolder .htaccess into root one (you will need to slightly modify your rule by fixing the path -- adding photography/ may be enough, depends on actual "photography" location)
UPDATE:
Your root .htaccess can be like this:
Options +FollowSymlinks
<IfModule mod_rewrite.c>
# activate rewrite engine
RewriteEngine On
# we are in the root
RewriteBase /
# no www please
RewriteCond %{HTTP_HOST} ^www\.notails\.com [NC]
RewriteRule ^(.*)$ http://notails.com/$1 [R=301,L,QSA]
# photography
RewriteRule ^photography/show/([^/\.]+)/([^/]+)$ /photography/show.php?section=$1&photoid=$2 [L]
# WordPress rules
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>