.htaccess issues- need to extract a new rewritebase and make a filename substitution into a variable

89 views Asked by At

I have 3 possible locations for a site to be viewed-

http://localhost/mysite
http://myhost.com/webs/mysite  (for demos prior to publishing)
http://clienturl.com

The files use a simple PHP templating system so that normally, the full URL for any given content would be- http://localhost/mysite/?pg=contact

However, what I wish to do is have

http://localhost/mysite/contact.html resolve so that the word "contact" gets placed within the variable "pg".

This works fine with this .htaccess directive-

RewriteRule (.*)\.html index.php?pg=$1 [QSA] # 

It works fine locally, and at my myhost.com.

However, when placed at clienturl.com, any page other than the root:
clienturl.com

causes a server error, similar to "cannot find the file."

So I discovered that I could specify an environment variable.

.htaccess

#0 http://stackoverflow.com/questions/869092/how-to-enable-mod-rewrite-for-apache-2-2 0#
# Tell PHP that the mod_rewrite module is ENABLED.
#0 --------------------------------------------- 0#
SetEnv HTTP_MOD_REWRITE On
#0 --------------------------------------------- 0#

# Determine REWRITEBASE #
#1 http://stackoverflow.com/questions/2045897/with-mod-rewrite-can-i-specify-a-rewritebase-within-a-rewritecond 1#
#1 https://www.webmasterworld.com/apache/3522649.htm 1#
#1 --------------------------------------------- 1#
# Activation of the URL Rewriting
Options +FollowSymlinks
RewriteEngine On

# RewriteBase equivalent - NOT portal2web.biz or local
RewriteCond %{HTTP_HOST} !^portal2web.biz$ [OR]     
RewriteCond %{HTTP_HOST} !^localhost$
RewriteRule . - [E=REWRITEBASE:/]


# RewriteBase equivalent - portal2web.biz or local
RewriteCond %{HTTP_HOST} ^portal2web.biz$ [OR]  
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule . - [E=REWRITEBASE:]

#1 --------------------------------------------- 1#

# The rest of your rewrite rules here

# rules should be written most complicated to least complicated, structurally
RewriteRule (.*)\.html %{ENV:REWRITEBASE}index.php?pg=$1 [QSA] # 

It turns out the clienturl.com requires "/" to be specified for the REWRITEBASE despite, as far as I know, the .htaccess supposedly allows for relative URLs and shouldn't need this to be specified explicitly.

This works at all locales except for my webserver, until I put a WWW in front of the URL, because, I think, of a directive handling WWW from my own server's root folder.

So I tried this-

.htaccess

# http://stackoverflow.com/questions/869092/how-to-enable-mod-rewrite-for-apache-2-2 #
# Tell PHP that the mod_rewrite module is ENABLED.
# --------------------------------------------- #
SetEnv HTTP_MOD_REWRITE On
# --------------------------------------------- #

# Determine REWRITEBASE #
# http://stackoverflow.com/questions/2045897/with-mod-rewrite-can-i-specify-a-rewritebase-within-a-rewritecond #
# https://www.webmasterworld.com/apache/3522649.htm #
# Activation of the URL Rewriting
Options +FollowSymlinks
RewriteEngine On

# check for http:// or https:// and set environment variable HTTP #
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ - [E=HTTP:http://]

RewriteCond %{HTTPS}:s on:(s) 
RewriteRule ^(.*)$ - [E=HTTP:https://]

# define environment variable REWRITEBASE #
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=REWRITEBASE:%{ENV:HTTP}%{HTTP_HOST}%1]

# rewrite to get proper variable format for index #
# rules should be written most complicated to least complicated, structurally
RewriteRule (.*)\.html index.php?pg=$1 [QSA] # 

This works locally, and at my webserver, meaning that contact.html is shown in the URL bar as contact.html, (even with a WWW in front of the URL) but at the clienturl.com, the url reverts back to index.php?pg=contact.

I'm confused, and I would really appreciate some help understanding what I'm doing wrong.

Thank you all for your help!

0

There are 0 answers