.htaccess redirect to error page if port is not 80

I’m running a portable server through usb stick. The thing is I also have WAMP installed in my local machine and Apache somehow gets started on windows startup, because of some random reason which I don’t recall now and it can’t be changed. I want to prepare my portable server in situations like this, so closing httpd.exe from process and starting my portable server is not an option. Anyway, because of already active httpd.exe my portable server’s WordPress site can only be accessed through localhost:81 - this is a problem as WP site is very dependent on the URL and I don’t want to include the url with port on WP database.

Here is what I want to do through .htaccess:

  • On any path except for error.php file check if not port 80
  • If not port 80 redirect to /error.php?code=port

It it possible for it to have priority over WP redirection or URL handling?

In the error.php I provided info on how to manually close httpd.exe and such so my family and friends can access the portable site. It’s sort of like a gallery and calender application for events and other such stuff…

Please help? I’m I can’t figure it out at all. I know others may not have apache already running, but I want to prepare for such a situation.

Something like the following, but the following doesn’t work.

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    	<If "%{SERVER_PORT} = 80">
    	RewriteEngine On
    	RewriteBase /
    	RewriteRule ^index\\.php$ - [L]
    	RewriteCond %{REQUEST_FILENAME} !-f
    	RewriteCond %{REQUEST_FILENAME} !-d
    	RewriteRule . /index.php [L]
    	</If>
    	<Else>
    	RewriteEngine On
    	RewriteRule ^(error.php)($|/) - [L]
    	RewriteRule ^(.*)$ /error.php?code=port [L]
    	</Else>
    </IfModule>
    # END WordPress

By the way, the portable server Server2Go automatically generates vhosts based o the hostname set on it’s config file and changes ports if the port (e.g. 80) is already open.

Someone suggested I try the following:

#  <If "%{SERVER_PORT} = 80">
RewriteEngine On
RewriteBase /
RewriteCond  %{SERVER_PORT} ^80$
RewriteRule ^index\\.php$ - [L]
RewriteCond  %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#</If>
#<Else>
RewriteEngine On
RewriteCond  %{SERVER_PORT} !^80$
RewriteRule ^(error.php)($|/) - [L]
RewriteCond  %{SERVER_PORT} !^80$
RewriteRule ^(.*)$ /error.php?code=port [L]
#</Else>

However, it’s not working though. When gone to sample.local:81 it keeps on loading on an infinite-like load and then goes to a blank sample.local. Any ideas?

lucid,

First, welcome to SitePoint!

Second, open services.msc and STOP Apache. That will solve the port 80 problem.

Third, OMG! 50Mb executable with the whole ball of wax? I’ll have to give this a try!

Fourth, I’ve resolved this same (type) of problem by configuring a notebook as a mirror of my development machine and taking that on client calls. Since I can configure Apache, PHP, MySQL (and PHPMyAdmin) to mimic my production server, that allows me to demonstrate the exact functioning of the websites I build to (perspective) clients. IMHO, that’s far cleaner than using a USB stick.

Here is what I want to do through .htaccess:

  • On any path except for error.php file check if not port 80
  • If not port 80 redirect to /error.php?code=port
RewriteEngine on
RewriteCond %{SERVER_PORT} !^80$
RewriteRule .? error.php?code=%{SERVER_PORT} [L]

It it possible for it to have priority over WP redirection or URL handling?

Of course! Place this code before the WP code.

THEN, please understand that the <IfModule> is EVIL as it abuses servers. WP includes it to prevent the clueless from hammering them for using code which returns 500 error code for every request (because the mod_rewrite code is not recognized). Use it without the <IfModule> wrappers and, if it causes a problem, remove it all.

There are other problem with WP’s standard code but you’re not up to that point yet.

You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It’s helped may members and should help you, too.

Regards,

DK