Mobile app version of vmapp.org
Login or Join
Shanna517

: How to write a generic .htaccess file rule, regarding RewriteBase The following rule in my .htaccess works just fine for routing calls to index.php: RewriteEngine On RewriteCond %{REQUEST_FILENAME}

@Shanna517

Posted in: #Apache #Htaccess #ModRewrite

The following rule in my .htaccess works just fine for routing calls to index.php:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]


However, this does not work when running on a temporary URL like example.com/~test/.
In such a case, the .htaccess file needs to read:

RewriteEngine On
RewriteBase /~test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]


Note the extra RewriteBase.

Question: is it possible to write a rule in .htaccess to "detect" whether to apply that RewriteBase?

The reason is the .htaccess if version controlled, and I'd like for it to work on multiple installations.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Shanna517

2 Comments

Sorted by latest first Latest Oldest Best

 

@Berryessa370

Trying the same and on my search I found that symfony2 frameworks .htaccess file is doing that at least it is stated in the file that it would. I tried to use it in my own setup but didn't work. Maybe that will help you guys somehow.

# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the app.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.

RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::2$
RewriteRule ^(.*) - [E=BASE:%1]

10% popularity Vote Up Vote Down


 

@Hamaas447

As far as I know, there's no reliable way to detect what the RewriteBase should be. If there were, there would be no need for the kluge that RewriteBase is, as mod_rewrite could just detect the correct base automatically.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme