1

In a nutshell, I want to be able to use htaccess so that I can execute various rewritte commands. I am using AWS elasticbeanstalk, and below is my dilemna when trying to get htaccess to work.

My instance have failed to be uploaded, and have received a red alert has shown in the image below: enter image description here

Essentially I have overwrite all using .ebextensions to allow htaccess to work with the following code:

files:          
  "/etc/httpd/conf.d/enable_mod_rewrite.conf": 
     mode: "644"
     owner: root
     group: root
     content: |
       AllowOverride All

but that's not whats causing problem. what runs problem is this:

RewriteEngine On

located in htaccess. almost like it cant accept rewritenegine being turned on.

Update:

In /var/log/eb-activity.log:

Command CMD-TailLogs succeeded.
[2015-07-10T10:25:09.784Z] INFO  [25589] - [CMD-TailLogs] : Starting activity...
[2015-07-10T10:25:10.206Z] INFO  [25589] - [CMD-TailLogs/AddonsBefore] : Starting activity...
[2015-07-10T10:25:10.207Z] INFO  [25589] - [CMD-TailLogs/AddonsBefore] : Completed activity.
[2015-07-10T10:25:10.207Z] INFO  [25589] - [CMD-TailLogs/TailLogs] : Starting activity...
[2015-07-10T10:25:10.207Z] INFO  [25589] - [CMD-TailLogs/TailLogs/TailLogs] : Starting activity...

In /var/log/eb-commandprocessor.log

   [2015-07-10T10:37:19.644Z] DEBUG [25873] : Checking if the command processor should execute...
[2015-07-10T10:37:19.645Z] DEBUG [25873] : Checking whether the command is applicable to instance (i-80bbbd77)..
[2015-07-10T10:37:19.645Z] INFO  [25873] : Command is applicable to this instance (i-80bbbd77)..
[2015-07-10T10:37:19.645Z] DEBUG [25873] : Checking if the received command stage is valid..
[2015-07-10T10:37:19.645Z] INFO  [25873] : No stage_num in command. Valid stage..
[2015-07-10T10:37:19.645Z] INFO  [25873] : Command processor should execute command.
[2015-07-10T10:37:19.645Z] DEBUG [25873] : Storing current stage..
[2015-07-10T10:37:19.645Z] DEBUG [25873] : Stage_num does not exist. Not saving null stage. Returning..
[2015-07-10T10:37:19.646Z] INFO  [25873] : Executing command: CMD-TailLogs...
[2015-07-10T10:37:19.646Z] DEBUG [25873] : Reading config file: /etc/elasticbeanstalk/.aws-eb-stack.properties
[2015-07-10T10:37:19.647Z] DEBUG [25873] : Refreshing metadata..
[2015-07-10T10:37:20.061Z] DEBUG [25873] : Refreshed environment metadata.
[2015-07-10T10:37:20.061Z] DEBUG [25873] : Retrieving metadata for key: AWS::ElasticBeanstalk::Ext||_ContainerConfigFileContent||commands..
[2015-07-10T10:37:20.062Z] DEBUG [25873] : Retrieving metadata for key: AWS::ElasticBeanstalk::Ext||_API||_Commands..
[2015-07-10T10:37:20.064Z] INFO  [25873] : Found enabled addons: ["logpublish"].
[2015-07-10T10:37:20.066Z] INFO  [25873] : Updating Command definition of addon logpublish.
[2015-07-10T10:37:20.066Z] DEBUG [25873] : Loaded definition of Command CMD-TailLogs.
[2015-07-10T10:37:20.066Z] INFO  [25873] : Executing command CMD-TailLogs activities...
[2015-07-10T10:37:20.066Z] DEBUG [25873] : Setting environment variables..
[2015-07-10T10:37:20.066Z] INFO  [25873] : Running AddonsBefore for command CMD-TailLogs...
[2015-07-10T10:37:20.067Z] DEBUG [25873] : Running stages of Command CMD-TailLogs from stage 0 to stage 0...
[2015-07-10T10:37:20.067Z] INFO  [25873] : Running stage 0 of command CMD-TailLogs...
[2015-07-10T10:37:20.067Z] DEBUG [25873] : Loaded 1 actions for stage 0.
[2015-07-10T10:37:20.067Z] INFO  [25873] : Running 1 of 1 actions: TailLogs...

update3 the content of my .htaccess file

RewriteEngine On
RewriteCond %{THE_REQUEST} /searchPage.php\?crs_category=([^\s]+) [NC]
RewriteRule ^ category/%1? [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^/]+)/?$  searchPage.php?crs_category=$1 [QSA,L,NC]
code_legend
  • 3,547
  • 15
  • 51
  • 95

1 Answers1

5

AllowOverride is valid only in <Directory> sections specified without regular expressions, not in <Location>, <DirectoryMatch> or <Files> sections.

Source: Apache Core Module

You put the AllowOverride in /etc/httpd/conf.d/enable_mod_rewrite.conf. It might cause the Apache failed to start.

I've just tried using default Elastic Beanstalk Sample PHP configuration (see Supported Platforms and Sample Application). I also add two new files into sample PHP app:

phpinfo.php

<?php
    phpinfo();

.htaccess

RewriteEngine on
RewriteRule ^/?info.html$ phpinfo.php [L]

The result is:

PHPInfo was displayed correctly using <code>mod_rewrite</code> /info.html

By default, Elastic Beanstalk PHP environment already enable the mod_rewrite and AllowOverride All to the PHP app directory.

I've also tried to add your .ebextensions script (add).

files:             "/etc/httpd/conf.d/enable_mod_rewrite.conf": 
     mode: "644"
     owner: root
     group: root
     content: |
       AllowOverride All

After it was deployed, the Apache server failed to start and I could not access the web. This was caused by wrong config. The error was that I've ever said in top of my answer.

The solution might be by removing your enable_mod_rewrite.conf and use the default environment.

UPDATE

I tried to use a simple searchPage.php and your .htaccess, but with a bit modified, you had an extra space between ^ and category in 2nd line.

.htaccess

RewriteEngine On
RewriteCond %{THE_REQUEST} searchPage.php\?crs_category=([^\s]+) [NC]
RewriteRule ^category/$1? [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^/]+)/?$ searchPage.php?crs_category=$1 [QSA,L,NC]

searchPage.php

<?php
    header('Content-Type: text/plain');
    print_r($_SERVER);
    print_r($_REQUEST);

I tried to do a curl:

$ curl -v 'http://default-environment-zjqxqumicq.elasticbeanstalk.com/searchPage.php?crs_category=test'

* Hostname was NOT found in DNS cache
*   Trying 54.173.237.116...
* Connected to default-environment-zjqxqumicq.elasticbeanstalk.com (54.173.237.116) port 80 (#0)
> GET /searchPage.php?crs_category=test HTTP/1.1
> User-Agent: curl/7.35.0
> Host: default-environment-zjqxqumicq.elasticbeanstalk.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: text/plain;charset=UTF-8
< Date: Tue, 21 Jul 2015 13:27:56 GMT
* Server Apache is not blacklisted
< Server: Apache
< Content-Length: 1490
< Connection: keep-alive
< 
Array
(
    [PHP_MEMORY_LIMIT] => 256M
    [PHP_MAX_EXECUTION_TIME] => 60
    [PHP_DISPLAY_ERRORS] => Off
    [PHP_COMPOSER_OPTIONS] => 
    [PHP_ALLOW_URL_FOPEN] => On
    [PHP_ZLIB_OUTPUT_COMPRESSION] => Off
    [PHP_DOCUMENT_ROOT] => /
    [PHP_DATE_TIMEZONE] => UTC
    [HTTP_HOST] => default-environment-zjqxqumicq.elasticbeanstalk.com
    [HTTP_ACCEPT] => */*
    [HTTP_USER_AGENT] => curl/7.35.0
    [HTTP_X_FORWARDED_FOR] => 149.129.122.128
    [HTTP_X_FORWARDED_PORT] => 80
    [HTTP_X_FORWARDED_PROTO] => http
    [HTTP_CONNECTION] => keep-alive
    [PATH] => /usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
    [SERVER_SIGNATURE] => 
    [SERVER_SOFTWARE] => Apache
    [SERVER_NAME] => default-environment-zjqxqumicq.elasticbeanstalk.com
    [SERVER_ADDR] => 172.31.5.238
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => 172.31.30.235
    [DOCUMENT_ROOT] => /var/www/html/
    [REQUEST_SCHEME] => http
    [CONTEXT_PREFIX] => 
    [CONTEXT_DOCUMENT_ROOT] => /var/www/html/
    [SERVER_ADMIN] => root@localhost
    [SCRIPT_FILENAME] => /var/www/html/searchPage.php
    [REMOTE_PORT] => 24890
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => crs_category=test
    [REQUEST_URI] => /searchPage.php?crs_category=test
    [SCRIPT_NAME] => /searchPage.php
    [PHP_SELF] => /searchPage.php
    [REQUEST_TIME_FLOAT] => 1437485276.635
    [REQUEST_TIME] => 1437485276
)
Array
(
    [crs_category] => test
)
* Connection #0 to host default-environment-zjqxqumicq.elasticbeanstalk.com left intact

and

$ curl -v 'http://default-environment-zjqxqumicq.elasticbeanstalk.com/category/test'                   

* Hostname was NOT found in DNS cache
*   Trying 52.5.119.104...
* Connected to default-environment-zjqxqumicq.elasticbeanstalk.com (52.5.119.104) port 80 (#0)
> GET /category/test HTTP/1.1
> User-Agent: curl/7.35.0
> Host: default-environment-zjqxqumicq.elasticbeanstalk.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: text/plain;charset=UTF-8
< Date: Tue, 21 Jul 2015 13:30:13 GMT
* Server Apache is not blacklisted
< Server: Apache
< Content-Length: 1918
< Connection: keep-alive
< 
Array
(
    [REDIRECT_PHP_MEMORY_LIMIT] => 256M
    [REDIRECT_PHP_MAX_EXECUTION_TIME] => 60
    [REDIRECT_PHP_DISPLAY_ERRORS] => Off
    [REDIRECT_PHP_COMPOSER_OPTIONS] => 
    [REDIRECT_PHP_ALLOW_URL_FOPEN] => On
    [REDIRECT_PHP_ZLIB_OUTPUT_COMPRESSION] => Off
    [REDIRECT_PHP_DOCUMENT_ROOT] => /
    [REDIRECT_PHP_DATE_TIMEZONE] => UTC
    [REDIRECT_STATUS] => 200
    [PHP_MEMORY_LIMIT] => 256M
    [PHP_MAX_EXECUTION_TIME] => 60
    [PHP_DISPLAY_ERRORS] => Off
    [PHP_COMPOSER_OPTIONS] => 
    [PHP_ALLOW_URL_FOPEN] => On
    [PHP_ZLIB_OUTPUT_COMPRESSION] => Off
    [PHP_DOCUMENT_ROOT] => /
    [PHP_DATE_TIMEZONE] => UTC
    [HTTP_HOST] => default-environment-zjqxqumicq.elasticbeanstalk.com
    [HTTP_ACCEPT] => */*
    [HTTP_USER_AGENT] => curl/7.35.0
    [HTTP_X_FORWARDED_FOR] => 149.129.122.128
    [HTTP_X_FORWARDED_PORT] => 80
    [HTTP_X_FORWARDED_PROTO] => http
    [HTTP_CONNECTION] => keep-alive
    [PATH] => /usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
    [SERVER_SIGNATURE] => 
    [SERVER_SOFTWARE] => Apache
    [SERVER_NAME] => default-environment-zjqxqumicq.elasticbeanstalk.com
    [SERVER_ADDR] => 172.31.5.238
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => 172.31.11.66
    [DOCUMENT_ROOT] => /var/www/html/
    [REQUEST_SCHEME] => http
    [CONTEXT_PREFIX] => 
    [CONTEXT_DOCUMENT_ROOT] => /var/www/html/
    [SERVER_ADMIN] => root@localhost
    [SCRIPT_FILENAME] => /var/www/html/searchPage.php
    [REMOTE_PORT] => 16148
    [REDIRECT_QUERY_STRING] => crs_category=test
    [REDIRECT_URL] => /category/test
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => crs_category=test
    [REQUEST_URI] => /category/test
    [SCRIPT_NAME] => /searchPage.php
    [PHP_SELF] => /searchPage.php
    [REQUEST_TIME_FLOAT] => 1437485413.02
    [REQUEST_TIME] => 1437485413
)
Array
(
    [crs_category] => test
)
* Connection #0 to host default-environment-zjqxqumicq.elasticbeanstalk.com left intact

It's worked.

Edward Samuel
  • 3,846
  • 1
  • 22
  • 39
  • 1
    thank you for your response and yes the response you receive after you deploy it with the config file is precisely what I get (the Apache server failed to start and I could not access the web.) if htaccess is enabled by default, my question is how come it doesnt work because it works fine in localhost. I have include an update 3 section under my initial post that include the htaccess code. the .htaccess file is located at the root of the project – code_legend Jul 20 '15 at 19:10
  • I thought you had an extra space in your `.htaccess`. Check on my updated answer above. – Edward Samuel Jul 21 '15 at 13:34
  • 1
    could you kindly explain your search page? is the content you wrote inside merely for testing purposes or is it necessary to get it to work? – code_legend Jul 21 '15 at 14:18
  • In `searchPage.php`, I only print out the `$_SERVER` and `$_REQUEST` variable. You can look at my code above. I thought you don't need add special thing in the page. Just make sure the file is exist and don't make a redirect loop. – Edward Samuel Jul 21 '15 at 14:24
  • I suggest you to create another simple php app with `mod_rewrite` in a new Elastic Beanstalk environment. You can use my code above as a sample if you want. – Edward Samuel Jul 21 '15 at 14:50
  • 2
    Sometime, a new environment could solve our problem. Because, we forgot something that we have changed in the old environment. – Edward Samuel Jul 21 '15 at 14:53
  • thanks for staying along. i've been wrapping my head around this for a few weeks. I've create a new file with a searchPage.php and .htaccess file – code_legend Jul 21 '15 at 14:53
  • and uploaded it as a zip for some reason it doesn't read the .htaccess file and get the following when i type category/business/ Not Found The requested URL /category/business/ was not found on this server. – code_legend Jul 21 '15 at 14:54
  • Could you access `/category/business`? – Edward Samuel Jul 21 '15 at 15:01
  • neither. how would i enable mod_rewrite WHILE creating the eb environemnt? – code_legend Jul 21 '15 at 15:04
  • It's enabled by default. If you want to load Apache module, just use `LoadModule` directive by creating a file using `.ebextensions`. But, you can check your `phpinfo()` first as my example in `phpinfo.php` above. If you find there is `mod_rewrite` there, the `mod_rewrite` is already on. – Edward Samuel Jul 21 '15 at 15:11
  • yes precisely i would like to use LoadModule, could you kindly elaborate on that? – code_legend Jul 21 '15 at 15:12
  • i've checked using phpinfo() and yes mod_rewrite is part of the loaded module – code_legend Jul 21 '15 at 15:17
  • 2
    Is there a `mod_rewrite` in phpinfo? Just to make sure, ssh to your EB EC2 instance and check the /etc/httpd/. Is the Apache config correct in there? – Edward Samuel Jul 21 '15 at 15:20
  • ngsyes there is a mod_rewrite in phpinfo. i have never manipulated eb ec2 instance apache config setti – code_legend Jul 21 '15 at 15:22
  • sorry for this newbieness but how i check /etc/httpd/ – code_legend Jul 21 '15 at 15:35
  • If you don't change anything, it should be ok. Try to run this: https://github.com/edwardsamuel/aws-eb-php-mod-rewrite-sample. Please use a new EB environment. – Edward Samuel Jul 22 '15 at 15:19
  • I thought when you failed to access `/category/business/` yesterday is caused by wrong zip format. When you open the zip file, it should be contain all your file (not a directory). If you notice from my example, I asked you to access `http://.elasticbeanstalk.com/aws-eb-php-mod-rewrite-sample-master/category/business/`. There is a directory `aws-eb-php-mod-rewrite-sample-master` when you open the [zip](https://github.com/edwardsamuel/aws-eb-php-mod-rewrite-sample/archive/master.zip) file. – Edward Samuel Jul 22 '15 at 15:47
  • thank you. your help has been tremendous and i appreciate the effort. One question and minor trouble i have is that it works when i type /category/business/ but i would like that when type searchPage.php?crs_category=business to be automatically rewritten (with an R=301 code to let search engine know). Also, why do i need to Create this environment inside a VPC (it wasn't configure as such previously) – code_legend Jul 28 '15 at 19:56
  • Look into this [question](http://stackoverflow.com/questions/13003319/htaccess-rewrite-query-string-as-path). I think it is same with you want. – Edward Samuel Jul 28 '15 at 23:47
  • Why use VPC instead of Classic environment? Take a look into http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-vpc.html#differences-ec2-classic-vpc. – Edward Samuel Jul 28 '15 at 23:48