Category Archives: php

How to remove query string variable from url

How to remove query string variable name from htaccess ? I have following structure.

http://localhost/newsite/category?id=News
http://localhost/newsite/category?id=Entertainment
http://localhost/newsite/category?id=Technology

I want to change

http://localhost/newsite/News/
http://localhost/newsite/Entertainment/
http://localhost/newsite/Technology/

like this. I tried rewriting in htaccess, but doesn't work

The utlimate way to make a single entry point (front controller) working with the most common web server setups

That's not gonna be easy, thanks to all reading this! :)

;TL;DR;

How can we know what is the 'DOCUMENT_ROOT' regardless the webserver (nginx/apache/apache with hattacces) setup.

Introduction

I'll try to be as descriptive as possible :)

It's all about forwarding all the requests to a single index.php file (excluding existing files). Yes, a pretty common tasks.

Project lives in: /var/www/html/point/point1

This might be both dev or prod env.

I want all the requests to go to "index.php" with the information about what actually was requested.

Details

Assuming the the project's host is 'point1" and it lives in /var/www/html/point/point1 -

When user enters http://point1/foo/bar - I want the webserver to run /var/www/html/point/index.php and send the info about requested URL to it (in this case /foo/bar).

I want to know the 'foo' and 'bar';

It's quite with vhosts on both apache and nginx Like in apache :

<directory /var/www/html/point/point1>
      RewriteEngine On
      RewriteCond %{SCRIPT_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^.*$ index.php
</directory>  

And nginx:

location /{
    server_name point1;
    root /var/www/html/point/point1;
    try_files $uri $uri/ /index.php$is_args$args
}
location ~ \.php$ {
 #...handle php
}

It's easy because what we can easily get the "remaining part" in the

 var_dump($_SERVER['REQUEST_URI']); 

The "REQUEST_URI" var tells us what is after the 'host'.

Fine.

Now the .htaccess comes in. Assuming the user has no access to the webserver settings, here is the typical way:

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

Or just RewriteRule ^(.*)$ index.php Yeah, I know there is a difference.

Problem

I want my script to handle all these situations. It's easy if we limited it to 'vhosts'. In this case the 'document root' is known. This why this works:

$request = $_SERVER['REQUEST_URI']);
$file =  $_SERVER['REQUEST_URI'][1];
$method =  $_SERVER['REQUEST_URI'][2];

We know that $file is now relative to $_SERVER['DOCUMENT_ROOT'];

Except that when I want to support .htaccess it isn't that easy. Because I don't know the "document_root".

What I get in "$_SERVER['REQUEST_URI']" is "/point/point1/foo/bar" and I don't really know what is the part responsible for "foo" and "bar";

It seems that in Symfony this part of code deals with it:

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L1826

But I'm not really sure.

What I have done so far is that I put into .htaccess this:

Hi.

Introduction

I'll try to be as descriptive as possible :)

It's all about forwarding all the requests to a single index.php file (excluding existing files). Yes, a pretty common tasks.

Project lives in: /var/www/html/point/point1

This might be both dev or prod env.

I want all the requests to go to "index.php" with the information about what actually was requested.

Details

Assuming the the project's host is 'point1" and it lives in /var/www/html/point/point1 -

When user enters http://point1/foo/bar - I want the webserver to run /var/www/html/point/index.php and send the info about requested URL to it (in this case /foo/bar).

I want to know the 'foo' and 'bar';

It's quite with vhosts on both apache and nginx Like in apache :

<directory /var/www/html/point/point1>
      RewriteEngine On
      RewriteCond %{SCRIPT_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^.*$ index.php
</directory>  

And nginx:

location /{
    server_name point1;
    root /var/www/html/point/point1;
    try_files $uri $uri/ /index.php$is_args$args
}
location ~ \.php$ {
 #...handle php
}

It's easy because what we can easily get the "remaining part" in the

 var_dump($_SERVER['REQUEST_URI']); 

The "REQUEST_URI" var tells us what is after the 'host'.

Fine.

Now the .htaccess comes in. Assuming the user has no access to the webserver settings, here is the typical way:

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

Or just RewriteRule ^(.*)$ index.php Yeah, I know there is a difference.

Problem

I want my script to handle all these situations. It's easy if we limited it to 'vhosts'. In this case the 'document root' is known. This why this works:

$request = $_SERVER['REQUEST_URI']);
$file =  $_SERVER['REQUEST_URI'][1];
$method =  $_SERVER['REQUEST_URI'][2];

We know that $file is now relative to $_SERVER['DOCUMENT_ROOT'];

Except that when I want to support .htaccess it isn't that easy. Because I don't know the "document_root".

What I get in "$_SERVER['REQUEST_URI']" is "/point/point1/foo/bar" and I don't really know what is the part responsible for "foo" and "bar";

It seems that in Symfony this part of code deals with it:

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L1826

But I'm not really sure.

What I have done so far is this, in .htaccess:

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
SetEnv USE_URI_PROTOCOL PATH_INFO

As you can see I am setting an environment var so I can use it later in my php single entry point:

$uriProtocol = getenv('USE_URI_PROTOCOL')?:'REQUEST_URI';
    $request = isset($_SERVER[$uriProtocol])?$_SERVER[$uriProtocol]:'';

    //getting only what's before ? (uri query string) if it exists 
    if (($pos = strpos($request, '?')) !== false){
        $request = substr($request, 0, $pos);
    } 

    //trimming slashes
    $request = trim($request, '/');
    $requestArray  = empty($request)? [] : explode('/', $request);

    var_dump('controller',$requestArray[0] ?? 'undefined');
    var_dump('method',$requestArray[1] ?? 'undefined');

It works. But something tells me it's not very cool.

I hope I'm clear here? It's about one-single-easy-solution-to-do-the-front-controller.

PHP – Uploaded file cannot be accessed/not found after it was uploaded

I am using this Simple-Ajax-Uploader plugin to upload files, then I used This PHP library to handle the processing of the files, to do things like generating random file names, resizing and specifying a directory to save the files, etc.

Here's the code:

<?php
require('../classes/class.upload.php');
require('../classes/User.php');

//Process a file uploaded via XMLHttpRequest
$handle = new upload($_FILES['uploadfile']['tmp_name']);
if ($handle->uploaded) {
    $handle->file_new_name_body = User::generateRandomString();
    $handle->image_resize = true;
    $handle->image_x = 360;
    $handle->image_ratio_y = true;
    $handle->process('temp-uploads');
    if ($handle->processed) {
        echo json_encode(array('success' => true, 'newfilename' => $handle->file_dst_name));
        $handle->clean();
    } else {
        exit(json_encode(array('success' => false, 'msg' => $handle->error))); 
    }
}

Well, the file will upload successfully but somehow it becomes inaccessible via any web page, it's also inaccessible if I try to access the "site-name.com/directory-name/filename.jpg" on the browser. It shows 404 Not found.

I also thought it could be file permission issues, but after giving the uploaded files including their directory) all possible permissions, they are still 404 Not Found. Right now, files uploaded by ajax are visible in cPanel file manager but no accessible in URL.

Finally, out of curiosity, I uploaded another file directly to the server via cPanel and that one was accessible, but the others still mysteriously remain inaccessible with 404 Not Found. I also renamed one the inaccessible files to test.jpg, but it remains inaccessible. I tried to search for similar problems here but can't find one.

Please what is going wrong here?

PHP – Uploaded file cannot be accessed/not found after it was uploaded

I am using this Simple-Ajax-Uploader plugin to upload files, then I used This PHP library to handle the processing of the files, to do things like generating random file names, resizing and specifying a directory to save the files, etc.

Here's the code:

<?php
require('../classes/class.upload.php');
require('../classes/User.php');

//Process a file uploaded via XMLHttpRequest
$handle = new upload($_FILES['uploadfile']['tmp_name']);
if ($handle->uploaded) {
    $handle->file_new_name_body = User::generateRandomString();
    $handle->image_resize = true;
    $handle->image_x = 360;
    $handle->image_ratio_y = true;
    $handle->process('temp-uploads');
    if ($handle->processed) {
        echo json_encode(array('success' => true, 'newfilename' => $handle->file_dst_name));
        $handle->clean();
    } else {
        exit(json_encode(array('success' => false, 'msg' => $handle->error))); 
    }
}

Well, the file will upload successfully but somehow it becomes inaccessible via any web page, it's also inaccessible if I try to access the "site-name.com/directory-name/filename.jpg" on the browser. It shows 404 Not found.

I also thought it could be file permission issues, but after giving the uploaded files including their directory) all possible permissions, they are still 404 Not Found. Right now, files uploaded by ajax are visible in cPanel file manager but no accessible in URL.

Finally, out of curiosity, I uploaded another file directly to the server via cPanel and that one was accessible, but the others still mysteriously remain inaccessible with 404 Not Found. I also renamed one the inaccessible files to test.jpg, but it remains inaccessible. I tried to search for similar problems here but can't find one.

Please what is going wrong here?

Load domain from a sub folder

I am new to windows hosting...

I have a hosting with 3 domains added to it. This is what I want to achieve:

My Main system url is http://xxx.xx.xx.x/winhost/hrm_test/

www.mydomain1.co.uk =>  http://xxx.xx.xx.x/winhost/hrm_test/STAFF_Area/*
www.mydomain2.co.uk =>  http://xxx.xx.xx.x/winhost/hrm_test/reports_manager/*
www.mydomain3.co.uk =>  http://xxx.xx.xx.x/winhost/hrm_test/support_team/*

the subfolders are not actually existing folders but post variables changed using web.config... is it possible to have addon domains to always request these subfolders

Load domain from a sub folder

I am new to windows hosting...

I have a hosting with 3 domains added to it. This is what I want to achieve:

My Main system url is http://xxx.xx.xx.x/winhost/hrm_test/

www.mydomain1.co.uk =>  http://xxx.xx.xx.x/winhost/hrm_test/STAFF_Area/*
www.mydomain2.co.uk =>  http://xxx.xx.xx.x/winhost/hrm_test/reports_manager/*
www.mydomain3.co.uk =>  http://xxx.xx.xx.x/winhost/hrm_test/support_team/*

the subfolders are not actually existing folders but post variables changed using web.config... is it possible to have addon domains to always request these subfolders

Import a wordpress website

Someone gave me a wordpress website (files and sql dump), I imported the database into mysql and the files onto my ftp. I modified the site and home url in the option table and opened the website.

The index page is fine but any link I click returns a 404. So I thought about .htaccess and tried the default one:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

But now instead of 404 I have a text like that:

Login
mysitename

How can I know what's the problem ?

WordPress on nginx – php file 403 error

I have WordPress installed on an nginx server. In my WP theme I have an _inc folder where I have a test.php file with a script inside that I need to access, but I get a 403 error.

My .httaccess file is like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Any ideas ?

Route not working in production

If try to access the index page in production it doesn't work, but in dev mode it works.

but if I add a path to content_homepage, it works fine in production, but I don't users to access the index page by adding /home to the url, they have to access to it directly (example.com) not -> (example.com/home)

routing.yml

content_homepage:
    path:     /
    defaults: { _controller: c8cac8cacontentBundle:Default:index }

controller

public function indexAction()
{
    return $this->render('c8cac8cacontentBundle:Default:index.html.twig');
}

Below my apache config:

<VirtualHost *:80>
    ServerName mywebsite.com
    ServerAlias www.mywebsite.com
    DocumentRoot /var/www/c8ca2/web
    <Directory /var/www/c8ca2/web>
        AllowOverride None
        Order Allow,Deny
        Allow from All

        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app.php [QSA,L]
        </IfModule>
    </Directory>

    ErrorLog /var/log/apache2/symfony_error.log
    CustomLog /var/log/apache2/symfony_access.log combined
</VirtualHost>

How do I rewrite this .htaccess file to remove index.php from CodeIgniter and force HTTPS?

This is the content of my .htaccess file.

I want to accomplish two things:

  1. Remove index.php from the CodeIgniter URL;
  2. Force all connections through https://

HTTPS works fine but index.php remains in the URL. How can I fix this?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]