Forum Moderators: phranque

Message Too Old, No Replies

How make Rewrite rules work in a diferent port than 80?

         

dragnovich

4:55 pm on Jun 27, 2009 (gmt 0)

10+ Year Member



Hello I have an apache working in my Windows PC (with the Apache from wampserver.com), anyway, I configure all the server and everithing works perfect even the .htaccess file and the rewrite rules it apply.

Then I added a second site, as is a local machine I cant make them use domains, or same port, so I use a vhost to serve that site but under port 1006. And all seems to work perfect, but the modrewirte rules in the vhost server, does not work....

this is an example, all calls to:
[localhost...]

are redirected to:
[localhost...]

But if i access the:
[localhost:1006...]
Got a 404 page not found

The root dirs are:
c:/sites/site1 & c:/sites/site2

Both sites has the say.php script and I tested and they work calling directly:
[localhost...]
[localhost:1006...]

Both sites has this .htaccess file:
RewriteEngine on
RewriteRule page/([^/\.]+)/?$ /say.php?page=$1 [L]

And this is the vhost configuration:
Listen *:1006
NameVirtualHost *:1006
<VirtualHost *:1006>
ServerName *
ServerAlias localhost
DocumentRoot "c:/sites/site2"
</VirtualHost>

and just in case, this is the say.php script:
<?php echo $_GET['page']; ?>

So why the modrewrite rules work under localhost:80 and the same file does not work under localhost:1006 ?

Note: running the phpinfo(); command reports that mod_rewrite is eneabled in both servers.

jdMorgan

7:33 pm on Jun 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> So why the modrewrite rules work under localhost:80 and the same file does not work under localhost:1006

Because port 80 is the de-facto HTTP port, and you are using a non-standard port.

You can indeed use "domains" on your local server, and I suggest that you do so, because forcing the non-standard port in mod_rewrite will make things too complicated when you decide to move the sites to 'real' hosting.

Find your "hosts" file -- That's it, just "hosts' with no file extention, and edit it with a plain-text editor (Windows Notepad will work fine) to add your domain names:
127.0.0.1 example.com
127.0.0.1 example2.com

Now you have sort of a "local DNS server" tht defines these two domains, and requests for those domains will now resolve to your local machine (localhost). Apache can now examine the HTTP Host header sent by the browser to determine which of your two name-based virtual hosts should handle the request.

If for some reason you decide that you must use the port number scheme, then the variable %{SERVER_PORT} will contain the port number of the request currently being processed, and you can test in in a RewriteCond or include it in your substitution URLs.

On WinXP, the hosts file is typically at c:\Windows\system32\drivers\etc\hosts

When you take the sites live with a published domain name in public DNS, simply delete that domain's entry from your hosts file.

Jim

dragnovich

6:42 am on Jun 28, 2009 (gmt 0)

10+ Year Member



Ok I try that too and same thing happens now rewrite_rules dont works on the new host names...

I add to c:\Windows\system32\drivers\etc\hosts:
127.0.0.1 testhost

then I add the vhost definition:
NameVirtualHost *
<VirtualHost *>
ServerName testhost
DocumentRoot "c:/sites/site2"
</VirtualHost>

I restart apache

and the two pages are displayed correctly but accesign
[testhost...]

Gives me again 404 not found

All paths are OK and all files are in place so... why the rules are not appying under the vhost?

dragnovich

8:10 am on Jun 28, 2009 (gmt 0)

10+ Year Member



It seems that .htaccess files on vhosts are not even been readed... I change the contents of the .htaccess to:

AuthUserFile c:/sites/site1/users.txt
AuthName "Please Log In"
AuthType Basic
require valid-user

That pop-up a Login dialog on access, at [localhost...] but it does not in [testhost...]

Is there any diretory option or directive that sould be added to the virtualhost definition to indicate apache that read the .htaccess files?

FYI: the users.txt file contents:
testuser:Ki4cezVjqTTwk

Caterham

9:02 am on Jun 28, 2009 (gmt 0)

10+ Year Member



Is there any diretory option or directive that sould be added to the virtualhost definition to indicate apache that read the .htaccess files?

AllowOverride [httpd.apache.org]

dragnovich

4:49 pm on Jun 29, 2009 (gmt 0)

10+ Year Member



thanks that made it work!

Regards!

Just if any one need it, this was the solution:

<VirtualHost *:80>
ServerName testhost
DocumentRoot "c:/sites/site2"
<Directory "c:/sites/site2">
AllowOverride All
</Directory>
</VirtualHost>