Remove index.php from URL after query params without using .htaccess - php

I want to remove index.php from my URL after query params.
This is my URL:
http://127.0.0.1/user/report?user=USERNAME
I have removed query params and convert it into pretty URL using:
RewriteCond %{QUERY_STRING} !user=
RewriteRule ^([a-zA-Z0-9\-]+)/(.*)$ $2?user=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9\-]+)$ ?user=$1 [L,QSA]
Now, my URL looks like this:
http://127.0.0.1/user/report/USERNAME
So all the requests to this URL will point to the entry script of my project i.e. web/index.php.
When I use below routes to get data, it works:
http://127.0.0.1/user/report/Default/index.php/api/registration/user-registrations/
But when I remove index.php from URL and access it like below, it throws 404:
http://127.0.0.1/user/report/Default/api/registration/user-registrations/
Apache config file:
Alias /user/report /path/to/project/web/
<Directory /path/to/project/web/>
AllowOverride All
Require all Granted
RewriteOptions AllowNoSlash
RewriteEngine On
RewriteBase /user/report/
RewriteOptions AllowNoSlash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^.]+[^\/]$ $0\/ [R]
RewriteCond %{QUERY_STRING} !user=
RewriteRule ^([a-zA-Z0-9\-]+)/(.*)$ $2?user=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9\-]+)$ ?user=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.(\d+)\.(bmp|css|cur|gif|ico|jpe?g|m?js|png|svgz?|webp|webmanifest|pdf)$ $1.$3 [L]
</Directory>
I am using Symfony for routing all my routes.

If you are using Apache 2.4, and do not want to to use .htaccess files (e.g. for performance reasons), the solution is simply to use a oneliner: FallBackResource.
You would only need this:
<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot /var/www/project/public
DirectoryIndex /index.php
<Directory /var/www/project/public>
AllowOverride None
Order Allow,Deny
Allow from All
FallbackResource /index.php
</Directory>
# optionally disable the fallback resource for the asset directories
# which will allow Apache to return a 404 error when files are
# not found instead of passing the request to Symfony
<Directory /var/www/project/public/bundles>
FallbackResource disabled
</Directory>
</VirtualHost>
This is even shown in Symfony's documentation.

Related

Codigniter htaccess error 404 at production

i am very begainer to server technologies.i know this question asked too many times but i really don't get it run.
it runs with domain.com/2.0/index.php/testbut when you remove index.php it throws
The requested URL /2.0/test was not found on this server.
Apache/2.2.15 (CentOS) Server at domain.com port 443
my project stucture
-
Public_html
--2.0(this is my project folder where ci can be located.it has sys,app,.htaccess)
--.htaccess
My domain
https://domain.com/2.0/
about the problem
i have two htaccess one at root mention above one is inside 2.0 folder
and the strange thing is what ever i write in the htaccess file it never effect anything
public_html/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
public_html/2.0/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
my httpd.conf
<VirtualHost *:443>
ServerAdmin info#domain.com
DocumentRoot /home/natural/public_html
ServerName api01.domain.net
ErrorLog /var/log/httpd/error_log
CustomLog /var/log/access_log common
SSLEngine on
SSLCertificateFile /home/natural/api01.domain.net.crt
SSLCertificateKeyFile /home/natural/api01.domoin.key
<Directory />
Options Indexes FollowSymLinks
Allow from all
AllowOverride None
</Directory>
</VirtualHost>
i search alot and follow all the answers by Stack overflow but no luck.
The directive "AllowOverride None" in httpd.conf will disable parsing of the .htaccess file within the directories specified (in this case /) -- Can you confirm that either of your .htaccess files are in fact being parsed?
If not, does modifying the Directory subsection within your httpd.conf file to:
<Directory />
Options Indexes FollowSymLinks
Allow from all
AllowOverride All
</Directory>
Solve the issue?
For more information: http://httpd.apache.org/docs/2.4/mod/core.html#allowoverride
I assume 2.0 is the new version of the site. It looks like /2.0/.htaccess is written as though it expects to be in the root folder. Try adding RewriteBase /2.0/ after engine on. You can remove it later when you replace the contents of the root folder with that of 2.0's.
The main problem is your # Handle Front Controller... rule. It'll capture any virtual URLs before they ever get to the 2.0 folder. Try changing it to this:
RewriteRule ^(?!2\.0(?:/|$)) index.php [L]
That will exclude the 2.0 folder from the current site's virtual URLs.
You do still need AllowOverride all as recommended by the other answer, and the directory should be the same as your DocumentRoot, not /.
Add this code inside root->.htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^(?!2\.0(?:/|$)) index.php [L]

CodeIgniter Route works over CIFS mount but not with local files

our dev environment at work uses a single centos machine with apache/php that serves different virtual hosts for both CIFS mounted and local directories. our application is built using CodeIgniter 2.0
we recently made a change to our Routes that is working fine for the virtual hosts serving the mounted drives, but the routes cannot be resolved for local files, resulting in a 404.
$config['base_url'] = "http://production.host.com"; // the production value of $_SERVER['SERVER_NAME'] essentially
routes:
$route['companyone'] = 'sso/platformsso/requestgenericsso/companyone';
$route['companyone/(:any)'] = 'sso/ptlatformsso/requestgenericsso/companyone/$1';
this works for the following scenario:
<VirtualHost 192.168.1.1:80>
ServerAdmin me#host.com
DocumentRoot "/var/mnt/a"
ServerName "a.tmbc.com"
ErrorLog logs/a.error.log
Alias ssaml /var/simplesamlphp/www
</VirtualHost>
<Directory "/var/mnt/a">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Directory populated using CIFS mount: mount -t cifs //1.2.3.4/a /var/mnt/a -o rw,username=un,password=pwd,uid=48 --verbose1
but does not work for this scenario:
<VirtualHost 192.168.1.1:80>
ServerAdmin me#host.com
DocumentRoot "/var/www/html/b"
ServerName "b.host.com"
ErrorLog logs/b.error.log
Alias ssaml /var/simplesamlphp/www
</VirtualHost>
<Directory "/var/www/html/b">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Directory populated using SVN checkout: svn checkout file:///var/svn/repo/trunk /var/www/html/b
.htaccess:
AddType text/x-component .htc
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Restrict unused HTTP methods
RewriteCond %{REQUEST_METHOD} !^(GET|POST|HEAD)
RewriteRule .* - [R=405,L]
# Forces SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^_sys.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_URI} ^_app.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^(.*).(pdf|exe|doc|mp4)$ /externaldownload/?fileLocation=$1.$2 [R,L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
any ideas of why this is so?
I think I understand your issue here. Problem seems to be on (:any) your route rule.
$route['companyone/(:any)'] = 'sso/ptlatformsso/requestgenericsso/companyone/$1';
A URL with companyone as the first segment of URL and anything for the second segment will be remapped to sso/ptlatformsso/requestgenericsso/companyone/$1
It seems like codeigniter passing $1 as a variable to the function companyone which means you are actually matching any route there to forced to rewrite.
How about condensing those to route rules and make a
$route['(.*)'] = "sso/ptlatformsso/requestgenericsso/companyone/$1";
or have only one rule there for routes such as
$route['companyone/(:any)'] = 'sso/ptlatformsso/requestgenericsso/companyone/$1';

How to rewrite URL in htaccess for specific folders in PHP

I would need assistance in rewriting URLS. Here are the given
Site URL: http://example.com:8000/myapp
Current htaccess
Allow from localhost
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
my site is in
/var/www/htdocs/myapp
virtual host configuration
<VirtualHost *:8000>
DocumentRoot "/var/www/htdocs"
ServerName example.com
ErrorLog "/var/log/apache2/custom/a_local_error_log"
CustomLog "/var/log/apache2/custom/a_local_access_log" common
<Directory "/var/www/htdocs">
Options Indexes FollowSymLinks
Require all granted
Order allow,deny
Allow from all
AllowOverride All
</Directory>
</VirtualHost>
Problem is that my assets are located in the same folder as the app but the code for including the assets are in absolute. Example:
/_js/jquery.js
/_js/myjs.js
Is it possible to rewrite it to:
/_js/myqpp/jquery.js
/_js/myapp/myjs.js
Thanks!
You can insert this rule just below RewriteEngine on:
RewriteRule ^(_js)/([^/.]+\.js)$ /$1/myapp/$2 [L,NC,R=302]
I believe something like the following should work for this:
RewriteRule ^/_js/jquery.js$ /_js/myapp/jquery.js [NC,L]
RewriteRule ^/_js/myjs.js$ /_js/myapp/myjs.js [NC,L]
And try that before your other rewrite rule.

Remove index.php from URL in code Igniter

i have read posts regarding this but that did not gave answer to my problem
i have set everything i found on forums of code igniter and SO's questions
currently i have following settings :
.htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|(.*)\.swf|images|robots\.txt|css|docs|cache)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 /application/errors/404.php
</IfModule>
and i also have set $config['index_page'] = '';
but this is not working there is not change in URL
if i type *http://localhost:8088/crud_demo/index.php/login* it works but if i type *http://localhost:8088/crud_demo/login* it shows Not Found Error
My Route config
$route['register'] = "register";
$route['manage'] = "manage";
$route['default_controller'] = "login";
$route['404_override'] = '';
Change AllowOverride None to AllowOverride All in my virtual host file at /etc/apache2/sites-available/000-default.conf in ubuntu
Change AllowOverride None to AllowOverride All in my virtual host file at /etc/apache2/sites-available/default.conf in windows
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride All <---- replace None with All
</Directory>
<Directory /var/www >
Options Indexes FollowSymLinks MultiViews
AllowOverride All <--- replace None with All
Order allow,deny
allow from all
</Directory>
....
Then put this code in .htaccess file in root folder in codeigniter
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
It will definately work.
RewriteRule ^crud_demo/(.*)$ crud_demo/index.php/$1
http://martinmelin.se/rewrite-rule-tester/ to test URL rewrites.
I think CodeIgniter has it's own way of routing URLs like other PHP frameworks. Take a look here: http://codeigniter.com/user_guide/general/routing.html
If crud_demo is a subfolder of your root dir then you should change
RewriteBase /
to
RewriteBase /crud_demo

How do I map different server names to differrent DirectoryIndex in PHP Symfony?

Symfony uses the following typical .htaccess file:
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
and I have this as my vhost:
<VirtualHost 127.0.0.1:80>
ServerName jobeet.loc
DocumentRoot "C:/wamp/www/jobeet/web"
DirectoryIndex index.php
<Directory "C:/wamp/www/jobeet/web">
AllowOverride All
Allow from All
</Directory>
Alias /sf "C:/wamp/lib/symfony-1.4.1/data/web/sf"
<Directory "C:/wamp/lib/symfony-1.4.1/data/web/sf">
AllowOverride All
Allow from All
</Directory>
The above works perfectly, but I want to know how to map and additional debugging url, http://jobeet.dev to automatically serve the frontend_dev.php file so I can use urls like:
http://jobeet.dev/jobs/...
instead of
http://jobeet.dev/frontend_dev.php/jobs/...
to map to the debug .php file in the framework.
I tried adding a duplicate of the vhost entry and simply changing the servername and directoryindex to
ServerName jobeet.dev
DirectoryIndex frontend_dev.php
but understandably this does not work, as I believe I would need to check the URL in the .htaccess to do this?
Can anyone offer some advice regarding this?
Thanks in advance!
:)
First add jobeet.dev as a ServerAlias in your current VirtualHost so it can share the same hosting configuration:
<VirtualHost 127.0.0.1:80>
ServerName jobeet.loc
ServerAlias jobeet.dev
DocumentRoot "C:/wamp/www/jobeet/web"
....
Don't forget to restart Apache when you're done.
Next, turn on no_script_name in your dev configuration in apps/frontend/config/settings.yml:
dev:
.settings:
no_script_name: true
Now your dev web controller (frontend_dev.php) won't show up in your auto-generated URLs (from link_to(), url_for(), etc).
Finally, set up a RewriteRule for your dev domain before your production controller comes in to play to route everything coming in at jobeet.dev to your dev web controller:
RewriteEngine on
...
...
RewriteCond %{HOST_NAME} ^jobeet\.dev$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ frontend_dev.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
That should do it.
I'd recommend you to map all needed hosts (jobeet.loc, jobeet.dev, etc) to SF_DIR/web, set index.php as dir-index (as you did) and in that file just run particular app with a particular env depending on $_SERVER['HTTP_HOST'].
Hope I described good to make an idea clear.

Categories