Archive for the ‘apache2’ Category

httpd.conf or .htaccess for apache WordPress redirect

Monday, December 13th, 2010

This is simply a note to save this block of code should I need it again.

The server in question is one of many cloud instances, and local development machines, in an environment using PHP, JAVA, Surf, among other technologies.

In short, it’s a pretty complex implementation/configuration…

Some URLs are being rewritten, others forwarded, directory crawling is off, but…

a legacy WordPress blog is being converted to clean/pretty URLs for SEO purposes

(which “look” like directory folder URLs)

(rather than WordPress relying on URL parameters by default)

so, along with editing the WordPress Setting for this,

the following was added to the httpd.conf file:

<IfModule modrewrite.c>

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /podcastInQuestionFolder/index.php [L]

</IfModule>

Of course, quite similar code in the .htaccess within that folder would accomplish a similar aim.

Using folder structure, URL, instead of parameters, setup a virtual host, apache httpd-vhosts.conf, and then grab the url via PHP

Wednesday, November 17th, 2010

This post is about creating a small application in a folder, that behaves like Zend, or you could also say that it behaves like a service, in that it uses (converts) the folder structure of the url for the application’s use, rather than passing it url parameters. Another common example of such a system is employed by WordPress when “clean urls” is turned on.

 

Instead of this: http://alexyz.com?someparameter=something

We want this: http://alexyz.com/something

 

PHP then acquires that last value (call it a “command” or an “action” or whatever you’d like) and proceeds…

 

First, setup an apache virtual host, so that everything after the folder your app will live in is redirected back to that folder’s index along with any following parameters.  You do this via httpd-vhosts.conf

 

example contents of httpd-vhosts.conf (there are several virtual host projects in this example, for independant Zend applications, the code relevant to this lesson is red):

 

# Setup Listening Port
NameVirtualHost *:80

# Ensure “localhost” is preserved
<VirtualHost *:80>
ServerName localhost
DocumentRoot “C:\xampp\htdocs”

<Location /html/bonus>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ – [NC,L]
RewriteRule ^.*$ /html/bonus/index.php [NC,L]
</Location>

</VirtualHost>

# Setup “components” Virtual Host
<VirtualHost *:80>
ServerName components
DocumentRoot “C:\xampp\htdocs\components\public”

<Directory “C:\xampp\htdocs\components\public”>
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
RewriteEngine off

<Location />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ – [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</Location>

</VirtualHost>

# Setup “myzendresource” Virtual Host
<VirtualHost *:80>
ServerName myzendresource
DocumentRoot “C:\xampp\htdocs\myzendresource\public”
<Directory “C:\xampp\htdocs\myzendresource\public”>
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

# Setup “datamodels” Virtual Host
<VirtualHost *:80>
ServerName datamodels
DocumentRoot “C:\xampp\htdocs\datamodels\public”
<Directory “C:\xampp\htdocs\datamodels\public”>
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

 

So, any Url after /bonus, in this example, like: http://www.alexyz.com/bonus/something

will be pointed back to /bonus/index.php by apache for us.

 

Next, we simply grab that last part of the url in our index.php (in this example it’s the value “something”).  There are many ways we could do this, the example here is a very basic implementation of one way we might grab that value and proceed…

 

< ? php

$parameters = explode(“/”, $_SERVER[“REQUEST_URI”]);
foreach($parameters as $param){
if($param != NULL) { // This won’t fire if there is a terminating slash, we don’t want to set the parameter to empty!

$bonus_parameter = $param;

}
}

 

// now we can proceed with index.php just as though we’d done this, but we haven’t…:

 

$param = $_GET[‘parameterName’];

 

 

Mac osx Snow Leopard httpd.conf last line error needs removing for virtualhosts to work

Friday, September 17th, 2010

reference: http://code.google.com/p/virtualhost-sh/issues/detail?id=9

when you uncomment v-hosts in httpd.conf, they won’t work until you fix that last line…