Home > Enterprise >  htaccess redirect POST message to php file in subfolder
htaccess redirect POST message to php file in subfolder

Time:12-21

I hope someone can help me to correct my htaccess problem in a shared hosting scenario, so I don't have access to httpd.conf. I am trying to redirect a POST to /process-dev.php to the file in the correct subfolder as shown below, but instead my POST gets redirected to index.php
The POST comes from the webpage itself (actual domain replaced with subdomain1.com) https:://www.subdomain1.com.

/
|- .htaccess
|- css
|- clientscripts
|- php
|- site
    |- subdomain1.com
        |- language
            |- default
                |- process-dev.php
                |- index.php

My htaccess file

<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

Options  FollowSymlinks
Options -Indexes

RewriteEngine on

# Enforce SSL
RewriteCond %{HTTPS} !=on
# Handle non-www URLs
RewriteCond %{HTTP_HOST} ^subdomain1\.com [NC,OR]
# Handle www URLs
RewriteCond %{HTTP_HOST} ^www\.subdomain1\.com [NC]
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} ^/?(process-dev|post-file-dev|post-image-dev)?$ [NC]
RewriteRule ^.*$ site/subdomain1.com/language/default/$1.php [L,QSA]

# Redirect all to the Application if not done already
RewriteCond %{REQUEST_URI} !^/?site/subdomain1\.com/language/default/index\.php [NC]

# or if request is a real file
RewriteCond %{REQUEST_FILENAME} !-f

# or if request is a real directory but not the root directory
RewriteCond %{REQUEST_URI} ^/?$ [OR]
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite the rest to the index.php file in your public folder
RewriteRule ^.*$ site/subdomain1.com/language/default/index.php [NC,L]

The partly output of print($_server)

map(
'ONECOM_DOMAIN_NAME'=>'subdomain1.com',
'ONECOM_DOMAIN_ROOT'=>'/customers/6/6/d/subdomain1.com/',
'SCRIPT_NAME'=>'/site/subdomain1.com/language/default/index.php',
'REQUEST_URI'=>'/process-dev.php',
'QUERY_STRING'=>'',
'REQUEST_METHOD'=>'POST',
'SERVER_PROTOCOL'=>'HTTP/1.1',
'REDIRECT_URL'=>'/process-dev.php',
'SCRIPT_FILENAME'=>'/customers/6/6/d/subdomain1.com/httpd.www/site/subdomain1.com/language/default/index.php',
'SERVER_NAME'=>'subdomain1.com',
'HTTP_REFERER'=>'https://subdomain1.com/',
'HTTP_ORIGIN'=>'https://subdomain1.com',
'HTTP_SCHEME'=>'https',
'HTTP_HOST'=>'subdomain1.com',
'HTTPS'=>'on',
'ONECOM_TMPDIR'=>'/customers/6/6/d/subdomain1.com//tmp',
'DOMAIN_NAME'=>'subdomain1.com',
'ONECOM_DOCUMENT_ROOT'=>'/customers/6/6/d/subdomain1.com/httpd.www',
'DOCUMENT_ROOT'=>'/customers/6/6/d/subdomain1.com/httpd.www',
'REDIRECT_STATUS'=>'200',
'REDIRECT_HTTPS'=>'on',
'REDIRECT_ONECOM_TMPDIR'=>'/customers/6/6/d/subdomain1.com//tmp',
'REDIRECT_ONECOM_DOMAIN_ROOT'=>'/customers/6/6/d/subdomain1.com/',
'REDIRECT_ONECOM_DOMAIN_NAME'=>'subdomain1.com',
'REDIRECT_DOMAIN_NAME'=>'subdomain1.com',
'REDIRECT_ONECOM_DOCUMENT_ROOT'=>'/customers/6/6/d/subdomain1.com/httpd.www',
'REDIRECT_DOCUMENT_ROOT'=>'/customers/6/6/d/subdomain1.com/httpd.www',
'PHP_SELF'=>'/site/subdomain1.com/language/default/index.php',
)

Based on Raxi suggestion I got it to work with this change

RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} ^/?(process-dev\.php|post-file-dev\.php|post-image-dev\.php)$ [NC]
RewriteRule ^.*$ site/subdomain1.com/language/default/%1 [L,QSA]

CodePudding user response:

The problem lies in

RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} ^/?(process-dev|post-file-dev|post-image-dev)?$ [NC]
RewriteRule ^.*$ site/subdomain1.com/language/default/$1.php [L,QSA]

You are using $1 but there is no grouped pattern in the RewriteRule (^.*$), you ment to use %1 to refer to a group in the earlier RewriteCond.

Also, i'm not sure if you really ment to put that second ? in the RewriteCond pattern ? I doubt you wanted that part of the uri to be optional in order to match this rule.

  • Related