Using .htaccess to rewrite to Wordpress subdirectory? - php

As opposed to many questions here in StackOverflow, I'm looking for something a little different. I have a Wordpress install in a subfolder of my domain called "blog". The main part of the website is a Magento website. I'm looking to take any instance where "blog" is part of the URI, and make sure it's untouched by the myriad of other RewriteRules in Apache.
As a few examples:
http://www.example.com/blog/wp-admin/
http://www.example.com/blog/
http://www.example.com/blog/save-money-groceries-without-coupons/
...should all direct to the Wordpress site found under /blog/
http://www.example.com/
http://www.example.com/plumbing/faucets.html
http://www.example.com/about/family-history.html
...should all direct to the Magento site that is currently found under the root directory
What is the best practice in writing the .htaccess file to achieve this result?

To redirect http://www.example.com/blog/wp-admin/ to http://www.blog.example.com/wp-admin/ you can add the following to the .htaccess file
RewriteEngine On
RewriteRule ^blog/(.*)$ http://www.blog.example.com/$1 [L,R=301]

I'm not an expert on setting up Apache servers but I think you want your WordPress install in a completely different folder to Magento and then use Alias in your .conf file. You can read about the Alias directive here.
For example in my Apache .conf files I have
Alias /blog/ "/full/path/to/wordpress/htdocs/"
This way the folder /full/path/to/wordpress/htdocs/ has its own .htaccess and its own index.php - which is what you are going to want for running WordPress without jumping through hoops.
Remember to restart Apache if you update the .conf file.

Related

How to make Magento run in my subdomain

I'm trying to build a Magento (1.9.3.1) staging site in a subdomain but can't get it to work properly. The site is working but no images, js or css files are being loaded. I've searched around but all the answers I've found don't work for me.
My main website structure is www.domain.com which has it's own website (not Magento) but then I have subdomain.domain.com with Magento installed.
Following another answer I tried editing .htaccess and added the following:
RewriteCond %{HTTP_HOST} ^subdomain\.domain.com$
RewriteRule ^/(.*) http://subdomain.domain.com/index.php [L]
but that didn't work. I tried different variations of that too.
Any ideas what I'm doing wrong? I have a dedicated server and the domain structure is:
/httpdocs
/subdomain.domain.com
where the main website root is in httpdocs. Could that be the issue?
The subdomain is set correctly in the core_config_data table as well.
Most packages like magento require root access.. this may be the issue.
Also, try to check the path to youre images.
Okay #Olcan set me in the right direction so thanks. It wasn't actually root access needed but directory permissions were not set correctly.
All the directory permissions were set to 750. Changed them to 755 and everything works. Didn't need to do anything with .htaccess either so I left it at the default
RewriteRule .* index.php [L]
and it works.

stop htaccess redirect

I accessed a 'superfolder's .htaccess file and accidentally added
Rewrite / http://google.com/
This was done using php. Now I can't access php files in any directory to revert the change.
Is there anything I can do in a subdirectory of root to stop the redirect inside that folder?
Thank you.
And please don't ask more details about the 'accident'.. stupid mistake
I contacted server admin, both laughed at it.. Still interesting though how to stop it redirecting although it shouldn't happen if I use mod_rewrite.
With mod_alias' Redirect, you're screwed. That directive is applied across the board, starting from the path-node where the htaccess file sits (if it's in an htaccess file), or the path-node of the <Directory> block that it sits in. So the only solution is to get an admin to remove it.
With mod_rewrite however, with an htaccess file, it doesn't act the same as within a <Directory> block. Rules inside an htaccess file in a path-node inside a directory has precedence over rules inside an htaccess file in the parent directory. So if you had 2 htaccess files:
/.htaccess:
RewriteEngine On
RewriteRule ^/? http://google.com/
and in /tmp/.htaccess:
RewriteEngine On
RewriteRule ^/?tmp/ http://stackoverflow.com/
And you go to http://yourdomain.com/tmp/, you'll get redirected to http://stackoverflow.com/ because the rules in the tmp directory has precedence over the rules in the parent directory. In face, the rules in the parent directory aren't applied at all unless you've used the RewriteOptions Inherit directive to inherit any rules from the parent directory.
Because of this, you can simply create an htaccess file with the following:
RewriteEngine On
Use FTP to upload it to your subdirectory, and upload the php file that you used to change the parent directory's htaccess file. Then just use your browser and go to that php file in the subdirectory.
Having simply turned on the rewrite engine in your subdirectory, without any rules, means:
I have mod_rewrite active in this directory
Since the rewrite engine is turned on in this directory, ignore all rules in the all parent directories.
Since the mod_rewrite ruleset is blank (no actual RerwiteRule's) nothing happens at all
Accessing this directory, eventhough the rewrite engine is on, mod_rewrite does nothing so it's as if the rewrite engine is turned off.
Sounds counter-intuitive, but that's just how it works.
Connect using your FTP client. Enable hidden files in your FTP client (try FileZilla, this one lets you do that). Delete .htaccess in your superdirectory. If this file has other data in it, copy it to your local computer, make changes and upload it again.
Here is some more help: http://www.intrepid.com.au/how-to-view-htaccess-with-filezilla/

.htaccess in subfolder

I install a new cms (phpmydirectory) in a subfolder. In the root there is a wordpress with friendly url's.
When I activate the friendly url's in phpmydirectory, I get an 404 error in the categories and locations, but no in the listings. The 404 error appear in the blog template and URL.
How fix the -htaccess so the subfolder read the .htaccess in the subfolder ando no the one in root?
Apache always reads all .htaccess files along the path to the root directory. This cannot be changed (except for disallowing use of .htaccess alltogether). The best you can get is making sure that rules for mod_rewrite in the parent directory do not apply. This might be possible using
RewriteRule ^(.*)$ $1 [L]
as the last rule in your phpmydirectory directory.
Do you have the subdirectory specified as the $BASE_URL in your defaults.php file for PMD? I'm operating with a similar setup right now, and did not have to make adjustments to the .htaccess file. However, its not competing with a Wordpress install in the root directory (its an older version of phpmydirectory instead), so that might be causing a different issue than my setup.

Local Apache Server mod_rewrite/pretty links issue

I've had to move from testing on the live server, to testing locally on a virtual apache server. I've installed XAMPP just fine, downloaded and installed the wordpress files and the database. Everything looks great! The local version of my homepage is identical to the live version. There's only one problem: the homepage is the only page that works. When I click on one of the links i.e. the "about" page (http://localhost/wordpress/about/), I am redirected to the xampp control panel (http://localhost/xampp).
I have a good feeling this has to do with a problem with the "pretty links"/mod_rewrite rules. I made sure I brought over the .htaccess file, and it contains the rewrite instructions. The wordpress database has the proper permalink structure, and the httpd.conf file has the "RewriteEngine on" and the "FollowSymLinks" directives enabled. There has got to be some sort of rewrite problem here, although I am not ruling out something else stupid I might have done. Thanks for all your help!
-E
*Here is what the .htaccess looks like:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
In the httpd.conf, change the
DocumentRoot "/path/to/your/app/wordpress"
also
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/path/to/your/app/wordpress">
This should work, the path is absolute.
And do you have load the:
LoadModule rewrite_module modules/mod_rewrite.so
in httpd.conf??
If your server version works with a domain name, i.e: http://domainname.com is equivalent that http://localhost/wordpress the rewrite rules will be differents. post the rules here.
The problem is that wordpress does not believe in relative paths for some reason. There is an assumption that wordpress is running from the server root not a directory under the root (e.g. /var/www/wordpress will not work, but /var/www/ will).
The problem is with the .htaccess file they provide. It should re-write it to index.php and not /index.php. Change that line in your config and it will work.
What happens is that it tries to actually go to the default document root (in my case /var/www/index.php, which does not exist since I am using http://localhost/worpress which is an alias for ~/projects/worpress). You can check your error log and it will tell you where it is trying to look for the index.php file (which will return a 404 error).
I can go on a rant about how stupid it is that they do that and how bad the whole software design of wordpress is. But I will spare you that :).

how do i hide /magento/ in my site's url?

A site has an existing system (lets call it mysite)
and the client asks to put in magento.
My directory structure goes something like this:
ROOT
-index.php (this is the app's main controller)
-.htaccess
/blog (runs wordpress)
/assets (current system's media folder)
/magento (this is where all magento files go)
Problem is if I set up magento and specify in the installation that base URL is http://example.com, magento loads up mysite.
Leaves me no choice but to setup magento with base URL set to http://example.com/magento/ and it runs perfectly.
However the client wants me to feel hell and asks me to hide magento in the URL.
I’m not really versed in .htaccess and I know only simple rewrite codes so I tried forwarding any HTTP requests that start with /magento to the magento folder and came up with:
RewriteCond %{REQUEST_URI} !^/magento(.*)
RewriteRule (.*) /magento/$1 [L]
Just when I thought it was working, mysite links all became unaccessible and forwards to the magento system displaying it's 404 page.
So, uhm, can I ask for help how to construct the .htaccess to hide the /magento/ on the URLs without affecting the current system aka mysite?
Because you have existing applications off the webroot, you cannot get away with using nothing instead:
### webroot/.htaccess
RewriteRule ^whatiwanttouseinsteadofmagento/(.*)$ magento/$1 [L]
From how I see the problem you will not be able to hide magento completely and use your site as well in the same time.
If you want Magento in the root of the public folder you should just point the virtualHost to your magento installation but this will let your blog and your main controller out of the public view. This is more or less the same with what you did by redirecting all calls in the .htaccess to magento folder.
What I suggest is to change the magento name to something more anonymous like "shopping" or "cart", and remember that a folder rename is preferable to a .htaccess file in terms of security and performance.
Let's look at it:
RewriteCond %{REQUEST_URI} !^/magento(.*)
So we're saying the condition is anything that is not /magento(.*), so everything but that directory? This would redirect everything, including your blog, assets, and any other directories.
Without specifying each and every file that needs to be redirected to the magento directory, there really is no easy way of doing it. I suppose you could redirect any file that does not contain a "/" in it and ends with the extension .php to the magento directory. That way only files in the root web directory will redirect to magento, but if you used other directories inside the magento directory you'd still need to add separate rules for them.
this answer comes very late but I guess you wanted something like
RewriteCond %{REQUEST_URI} !^/(blog|assets|magento)(.*)$
RewriteRule ^(.*)$ /magento$1 [L]

Categories