I've recently come across a very strange problem with my Apache instance that I can't seem to find an answer to, I could be looking in the wrong places.
For my website I always run all requests through my index.php (All other content JS/CSS/PNG goes through a separate CDN), so I use my .htaccess to redirect all requests like so
SetEnv SERVER_ADMIN admin#example.com
SetEnv PHPRC /home/user
ServerSignature email
AddDefaultCharset UTF-8
DefaultLanguage en-US
# Enable Rewriting
RewriteEngine on
RewriteBase /
Options +FollowSymlinks
Options All -Indexes
# Do not redirect these directories
RewriteRule sitemap.xml - [L]
RewriteRule robots.txt - [L]
# Redirect to index
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^.*$ index.php [L]
I read the path provided from the $_SERVER variable, $_SERVER['REQUEST_URI']
I recently came across an issue that when I attempt to send any REQUEST_METHOD to my webserver that the request becomes a GET, but only if my URL contains an escaped / (%2F). While debugging this issue I print_r'd my $_SERVER variable to find the following:
A DELETE request to example.com/%2F
(Just a note that this isn't a trailing / issue, the issue happens no matter where it is in the URL, and I'm not unescaping it in my code)
{
"REDIRECT_REDIRECT_REDIRECT_REQUEST_METHOD": "DELETE",
"REDIRECT_REDIRECT_REDIRECT_STATUS": "200",
"REDIRECT_REDIRECT_SERVER_ADMIN": "admin#example.com",
"REDIRECT_REDIRECT_PHPRC": "/home/user",
"REDIRECT_REDIRECT_HTTPS": "on",
"REDIRECT_REDIRECT_SSL_TLS_SNI": "www.example.com",
"REDIRECT_REDIRECT_STATUS": "200",
"REDIRECT_HTTP_AUTHORIZATION": "",
"REDIRECT_SERVER_ADMIN": "admin#example.com",
"REDIRECT_PHPRC": "/home/user",
"REDIRECT_HTTPS": "on",
"REDIRECT_SSL_TLS_SNI": "www.example.com",
"REDIRECT_STATUS": "200",
"HTTPS": "on",
"SSL_TLS_SNI": "www.example.com",
"HTTP_HOST": "www.example.com",
"REQUEST_METHOD": "GET",
"REQUEST_URI": "/%2F",
"SCRIPT_NAME": "/index.php"
}
A DELETE request to example.com
{
"REDIRECT_REDIRECT_SERVER_ADMIN": "admin#example.com",
"REDIRECT_REDIRECT_PHPRC": "/home/user",
"REDIRECT_REDIRECT_HTTPS": "on",
"REDIRECT_REDIRECT_SSL_TLS_SNI": "www.example.com",
"REDIRECT_REDIRECT_STATUS": "200",
"REDIRECT_SERVER_ADMIN": "admin#example.com",
"REDIRECT_PHPRC": "/home/user",
"REDIRECT_HTTPS": "on",
"REDIRECT_SSL_TLS_SNI": "www.example.com",
"REDIRECT_STATUS": "200",
"HTTPS": "on",
"SSL_TLS_SNI": "www.example.com",
"HTTP_HOST": "www.example.com",
"REQUEST_METHOD": "DELETE",
"REQUEST_URI": "/",
"SCRIPT_NAME": "/index.php"
}
I've yet to find out why this escaped symbol is causing an additional redirect, and as to why it's changing the request method. I came across one suggestion on SO to replace my RewriteRule [L] to a RewriteRule [P] to proxy the request, but that only caused errors. The RewriteRule Flags documentation states that the proxy rule should be used with an external URL.
Any suggestions and/or help would be greatly appreciated.
Finally solved the problem with some further digging through StackOverflow.
First idea was just to read the $_SERVER['REDIRECT_REDIRECT_METHOD'] but I would NOT recommend doing this, as it turns out that Apache was also forcing a 404 response code during this redirect, meaning any URLs with an encoding %2F were returning as Not Found (Even with my page content showing up).
Answer came from this other question.
Solution:
# Set this rule in .htaccess/httpd.conf
AllowEncodedSlashes NoDecode
Related
My Website (opencart) works great on mywebsite.com but i got problems when trying www.mywebsite.com,
eg: icons on header and products disappear.
I tried to add a line on config.php:
<?php
// HTTP
define('HTTP_SERVER','http://mywebsite.com/');
// HTTPS
define('HTTPS_SERVER', 'http://mywebsite.com//public_html/');
// DIR
define('DIR_APPLICATION', '/home/XXXX/public_html/catalog/');
define('DIR_SYSTEM', '/home/XXXX/public_html/system/');
define('DIR_DATABASE', '/home/XXXX/public_html/system/database/');
define('DIR_LANGUAGE', '/home/XXXX/public_html/language/');
define('DIR_TEMPLATE', '/home/XXXX/public_html/view/template/');
define('DIR_CONFIG', '/home/XXXX/public_html/system/config/');
define('DIR_IMAGE', '/home/XXXX/public_html/image/');
define('DIR_CACHE', '/home/XXXX/public_html/system/cache/');
define('DIR_DOWNLOAD', '/home/XXXX/public_html/download/');
define('DIR_LOGS', '/home/XXXX/public_html/system/logs/');
// DB
define('DB_DRIVER', 'mysql');
define('DB_HOSTNAME', 'localhost');
define('DB_USERNAME', 'XXXX_com1');
define('DB_PASSWORD', 'XXXXXXXXX');
define('DB_DATABASE', 'XXXX_com1');
define('DB_PREFIX', 'oc_');?>
define('HTTP_SERVER','http://www.mywebsite.com/');
under
define('HTTP_SERVER','http://mywebsite.com/');
nothing happened.
i'm beginner on opencart, How can I solve this ?
Opencart only works over 1 url,
You have never been a opencart website working with two different url, to fix that your will have to include the following in your htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourDomain\.com
RewriteRule (.*) http://yourDomain.com/$1 [R=301,L]
This code redirect, all urls with www to urls without www.
This fix your problem.
The source
For my family members I was giving each person their own subdomain
(sister1.mydomain.com, sister2.mydomain.com, etc...)
I was using PHP to detect the domain, and then I'd load information related to the subdomain dynamically.
I'd like to get rid of the subdomains and use the power of .htaccess
My goal is to give the same URL:
www.mydomain.com/sister1
www.mydomain.com/sister2
www.mydomain.com/mommy
www.mydomain.com/daddyo
Obviously, I don't plan to have literal working directories for each person.
I'd pass the "sister1" portion to a process.php script that takes care of the rest.
I've figure out how to do it by manually typing each RewriteRule in my htaccess file:
Options +FollowSymLinks
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteRule ^/?sister1$ process.php?entity=sister1 [L]
RewriteRule ^/?sister2$ process.php?entity=sister2[L]
RewriteRule ^/?mommy$ process.php?entity=mommy[L]
RewriteRule ^/?daddyo$ process.php?entity=daddyo[L]
I feel this is the long way of doing it.
Is there a more universal way of extracting the text after the first "/" forwardslash, and passing it to process.php?entity=$1 ?
I tried it this way:
RewriteRule ^/([A-Za-z0-9-]+)/?$ process.php?entity=$1 [NC,L]
I'm getting the apache 404 error: "Not Found".
It is because you have a mandatory / in the beginning of your rule, i.e., you are always looking for something like /sibling in the URL. Your first examples have that first forward slash as optional due to the question mark after it.
You do not need a beginning forward slash - normally the rewrite rule picks up stuff after the domain name
www.example.com/string/mod/rewrite/gets/is.here
So just remove the starting slash and it should work.
I have searched on google and here for a tutorial to help me rewrite my urls.
I would like somebody to explain what I must write and why.
I have this url:
http://iescup.eu/tournaments.php?tourney[id]=1
http://iescup.eu/tournaments.php?tourney[id]=2
http://iescup.eu/tournaments.php?tourney[id]=3
and so on
I would like to have this url:
http://iescup.eu/#!/tourneys/1
http://iescup.eu/#!/tourneys/2
http://iescup.eu/#!/tourneys/3
and so on
Sincerely
Rune Naundrup Dahl
So you type http://iescup.eu/tournaments.php?tourney[id]=1 in your browser's URL address bar. The request /tournaments.php?tourney[id]=1 gets sent to the server iescup.eu. On that server, these rules in the htaccess file in the document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^tourney\[id\]=([0-9]+)$ [NC]
RewriteRule ^/?tournaments.php$ /#!/tourneys/%1? [L,R=301,NE]
The %{QUERY_STRING} variable is matched against and the numeric ID is grouped and backreferenced by %1. The rule's target has a ? at the end to remove the query string and the NE flag is used so the # doesn't get encoded.
The rule redirects the browser to http://iescup.eu/#!/tourneys/1 thus changing the URL address bar. The browser then sends another request to iescup.eu, /. Note that the #!/tourneys/1 fragment is never sent to the server. Fragments are client side only and is used to determine how content should be dealt with (also used by javascript).
I'm developing a multilanguage web app with Yii.
I applied changes to hide the index.php, changed urlFormat to path and added to the url path a slug with the user language example /it/index.php /en/index.php etc...
The problem now is that I need to redirect automatically to a different url once the user chooses another language. For example:
http://localhost/~antonio/project/it/women
needs to redirect to:
http://localhost/~antonio/project/it/femme
I have been playing with htaccess with no luck at all. Here is the actual code:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteBase /~antonio/project/
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
#My redirection code (tried a good few more to no use apart from this)
RewriteRule ^it/women$ it/femme
I would really appreciate any help on this issue, as it is driving me mad.
Thanks
Edit::
I surrendered with mod_rewrite. I found another solution by adding this code to /layout/main.php:
<?php
$onurl = Yii::app()->getRequest()->requestUri;
if ($onurl == "/~antonio/project/it/women") {
$this->redirect("/~antonio/project/it/femme");
} elseif ($onurl == "/~antonio/project/it/men") {
$this->redirect("/~antonio/project/it/uomme");
}
Rinse and repeat per combination of language/word
This might not work without setting up a proper Virtual Host (so that instead of local urls like http://localhost/~antonio/project/it/women you have nice urls like http://project1.dev). But I would do that anyway, since it makes your dev environment nicer! (Here's a place to start).
Anyway, I would try this: just leave the .htaccess file set like you normally would for "path" style urls, and then just parse a $_GET['lang'] parameter using the UrlManager? So you would have a urlManager setup like this in your config.php:
'urlManager'=>array(
'urlFormat'=>'path', // use path style parameters
'showScriptName'=>false, // get rid of index.php
'rules'=>array(
// this parses out the first chunk of url into the "lang" GET parameter
'<lang:[\w\-]+>/<controller:[\w\-]+>/<action:[\w\-]+>'=>'<controller>/<action>',
)
)
This way a url like this http://project1.dev/it/controller/action will redirect to the "action" action in your Controller like normal (controller/action), but in your action $_GET['lang'] will now have the value of "it".
I hope that helps, it's kind of a shot in the dark. I'm not sure how you are actually triggering the different languages, so a $_GET parameter might not be helpful after all.
Found a way to do this in htaccess:
RewriteCond %{REQUEST_URI} ^/~antonio/project/it/donna/shoes/(.*)$
RewriteRule ^it/donna/shoes/(.*)$ /~antonio/project/it/donna/calzature/$1 [L,R]
RewriteRule ^groups/([0-9+]*)/(.*)$ /users.php?group=$1 [QSA,L,E]
www.mysite.com/groups/11/all-users
in users.php i try get group id:
echo $_GET['group'];
Why always get "false"?
Thanks
I heartily recommend using the RewriteLog functionality for debugging your rewrite rules, I find it helps immensely to demystify what's happening inside.
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritelog
From what I can tell, you're merely missing the leading slash in the URL. I tested locally using (note the added leading slash before groups):
RewriteRule ^/groups/([0-9+]*)/(.*)$ /users.php?group=$1 [QSA,L,E]
The resulting page does a vardump of $_GET, providing:
array(1) { ["group"]=> string(2) "11" }
Try with this:
Example:
The original URL: http://www.mysite.com/users.php?group=1
The rewritten URL: http://www.mysite.com/group/1
RewriteEngine On
RewriteRule ^group/([^/]*)$ /users.php?group=$1 [L]