Remove .php extension in .htaccess not working - php

I have a test website on my ubuntu computer, the URL is: mysite.test. I have marketing pages: http://mysite.test/hello.php, http://mysite.test/goodbye.php, etc.
I also have wildcard subdomains: http://user1.mysite.test/page1.php, http://user2.mysite.test/page.1php, etc.
I'd like all pages to be accessible without adding the ".php" extension (marketing site, and subdomains). I've modified my .htaccess files, but its not working and I can't figure out why. mod_rewrite is enabled and working (the "Remove www" rule below works).
#/var/www/mysite.test/public_html/.htaccess
RewriteEngine On
#Remove www - this works
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC]
#Not need .php extension - this does not work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
Removing the first rule ("Remove www") doesn't make it start working.
When I go to http://mysite.test/hello I get a 404 Not Found error. Going to http://mysite.test/hello.php loads just fine.
When I go to http://user1.mysite.test/page1 I get a 404 Not Found error. Going to http://user1.mysite.test/page1.php loads just fine.
My Virtual Hosts file looks like this:
#/etc/apache2/sites-available/mysite.test.conf
<VirtualHost *:80>
ServerName www.mysite.test
ServerAlias mysite.test
ServerAdmin fake#gmail.com
DocumentRoot /var/www/mysite.test/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/mysite.test>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName wildcard.mysite.test
ServerAlias *.mysite.test
DocumentRoot /var/www/mysite.test/public_html/app
<Directory /var/www/mysite.test/public_html/app>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
What am I missing? Thank you.

I'm not sure I can give you a helpful solution to your issue, but I have a similar piece of code that works:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.|/]+)$ $1.php [NC,L]
this sets that if it is not a file, and if it doesn't contain a . (such as an image reference.jpg then it has .php appended to the filename.
This works perfectly for me.

Related

Hot to setup 2 different wordpress project with same domain on AWS Linux

currently i want to setup 2 different Wordpress project on my AWS Linux 2. but i still want to use a same domain. but i stuck.
1st Wordpress project use domain http://domainname.com
2nd wordpress project use domain http://domainname.com/second
what i have done is like this
1st Wordpress project
i set the .htaccess with this.. this setting is avoid 404 page when access http://domainname.com/second
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/second/.*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ./ /index.php [L]
</IfModule>
i also create a file "domainname.conf" on folder "/etc/httpd/conf.d/" with this setting
<VirtualHost *:80>
ServerName domainname.com
ServerAdmin email#gmail.com
DocumentRoot "/var/php_apps/domainname"
<Directory "/var/php_apps/domainname">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
ErrorLog /var/log/apache2/bar.error.log
LogLevel warn
CustomLog /var/log/apache2/bar.access.log combined
</VirtualHost>
this setting can load the page corretly. but i still can't display the 2nd wordpress project.. here is my setting
2nd Wordpress project
i set the .htaccess file with this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
i create a file "second.conf" on folder "/etc/httpd/conf.d/" with this setting
<VirtualHost *:80>
ServerName domainname.com/second
ServerAdmin email#gmail.com
DocumentRoot "/var/php_apps/second"
<Directory "/var/php_apps/second">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
ErrorLog /var/log/apache2/bar.error.log
LogLevel warn
CustomLog /var/log/apache2/bar.access.log combined
</VirtualHost>
but the 2nd wordpress project can't load the page when access domain http://domainname.com/second
error display
Not Found
The requested URL was not found on this server.
how do i able load the page from 2nd wordpress project when access domain http://domainname.com/second ?
please help.

setting .htaccess file in slim

I am having issues redirecting to index.php in my slim app with .htaccess file.
Routes work if I add the index.php at the end of the URL
So slimapp.dev/hello/myname gets error
Not Found The requested URL /hello/myname was not found on this server.
while slimapp.dev/index.php/hello/myname works
Here is my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.php [QSA,L]
I am using Apache 2 on Ubuntu 18.04
File structure
public_html
|_index.php
|_ vendor
|_.htaccess
virtualHost 000-default.conf
<VirtualHost *:80>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
If I use the php -S localhost:3000 it works as expected but if I use the Apache web server, I most add the index.php to the end of the URL to make it work. Thanks
You are doing wrong.This is quite simple with just two lines of code:
RewriteEngine On
RewriteRule ^myname/(.*)$ /$1 [L]

Rewrite subdirectory url to file

I have a subdirectory called test, http://www.mywebsite.com/test/. Inside of the directory is an index.php file.
I want to put an .htaccess file inside of the test subdirectory that will rewrite the URLs so that http://www.mywesbite.com/test/paul will become http://www.mywebsite.com/test?author=paul, and so that urls like http://www.mywebsite.ocom/test/sam/3 will become http://www.mywebsite.com/test?author=sam&test=3
This is what I have so far:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_URI} (/test/)
RewriteRule ^(.*)/(.*)/(.*)/?$ /test/index.php?author=$1&page=$2 [L]
RewriteCond %{REQUEST_URI} (/test/)
RewriteRule ^(.*)/(.*)/?$ /test/index.php?author=$1 [L]
Unfortunately, I get a 404 Not Found error. Am I missing something? Any thoughts are much appreciated!
EDIT: I checked phpinfo() and mod_rewrite does seem to be showing as a loaded module.
This is what my virtual host file looks like too. It looks like AllowOverride is set as well.
<VirtualHost *:80>
ServerName mywebsite.com
ServerAlias www.mywebsite.com
DocumentRoot /var/www/html/mywebsite.com/httpdocs
ErrorLog /var/log/apache2/mywebsite.com-error_log
CustomLog /var/log/apache2/mywebsite.com-access_log combined
HostnameLookups Off
UseCanonicalName Off
ServerSignature On
ScriptAlias /cgi-bin/ "/var/www/html/mywebsite.com/httpdocs/"
<Directory "/var/www/html/mywebsite.com/httpdocs/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<IfModule mod_userdir.c>
UserDir public_html
Include /etc/apache2/mod_userdir.conf
</IfModule>
</VirtualHost>
EDIT 2: I also wanted to mention, I put some junk text in the .htaccess file to try and break it, and instead of a 404 error I got an internal server error, so it does seem like the .htaccess file is being read.
I tested Panama Jack's solution and it seems like the RewriteRules are not accounting for this already being in /test/.htaccess... so this would not work for the first example (/test/paul). Proposing an alternate solution that should work for both examples:
RewriteEngine On
RewriteBase /test/
RewriteRule ^(.+)/(.+)/?$ index.php?author=$1&page=$2 [L]
RewriteCond %{REQUEST_URI} !index\.php$
RewriteRule ^(.+)/?$ index.php?author=$1 [L]

Laravel 5.1 .htaccess not working (pretty url's)

So I have tried both versions of the .htaccess, the one that comes with it in project creation and the one in the documentation. I am currently using the one in the documentation:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
I have my virtualhost set up like so:
<VirtualHost *:80>
ServerAdmin <my email>
ServerName projectflyer.<my domain>.com
DocumentRoot /var/www/html/projects/project-flyer/public
DirectoryIndex index.php
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
However when I go to projectflyer.mydomain.com/some/route I get back a 404. Any ideas? Yes I can go to the base url just fine.
I solved this by adding this to my virtualhost .conf file:
<Directory /path/to/your/install/public/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Thanks to http://laravel.io/forum/09-15-2015-removing-indexphp-from-url-laravel-5116 for the answer

wamp local subdomains and htaccess rewrite rules

i recently fixed my site up with a bit up php mvc magic for which i use those settings in my .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-zA-Z0-9]*)/?([a-zA-Z0-9]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&arg=$3 [NC,L]
it worked like a charm. the only problem i encountered was that when i reference files like css or images relatively, the browser assumed that we moved down the folder structure due to the dashes in the url
localhost/projectname/controllername/action/id
so the css/styles.css was requested at localhost/controllername/action/id/css/styles.css
having absolute paths fixed that problem but didnĀ“t work well with having a localtest server and a remote live and development server after there was no "projectname"- folder
i decided to configure my local wamp server so i can access my different project as a subdomain to my localhost like "http://projectname.localhost".
i activated the relevant apache modules, edited my hosts file and changed my httpd-vhosts.conf to
NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost.com
ServerAlias www.localhost.com
DocumentRoot "C:\wamp\www"
ErrorLog "logs\errors.log"
<directory "C:\wamp\www">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</directory>
</VirtualHost>
<VirtualHost *:80>
ServerName localhost.com
ServerAlias *.localhost.com
VirtualDocumentRoot "C:\wamp\www\subdomains\%1"
ErrorLog "logs\errors.log"
<directory "C:\wamp\www\subdomains\%1">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</directory>
</VirtualHost>
now when i call projectname.localhost.com/controllername/action/id it tells me
The requested URL /subdomains/projectname/index.php was not found on this server.
while projectname.localhost.com/index.php still supplies me with the right file.
i have heard that there might be a problem with the $_SERVER['DOCUMENT_ROOT'];. that doesn't get set properly but knowing that has not helped me fix that problem yet.
can someone point me into the right direction or suggest another environment configuration that makes testing under those circumstances possible?
changing my .htaccess to
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-zA-Z0-9]*)/?([a-zA-Z0-9]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&arg=$3 [NC,L]
solved that issue.
the magic line is RewriteBase /
seems like the server was really looking for the index.php in the wrong place.
hope this helps other people having the same problem... :)

Categories