Add real path in url rewriting - php

When I try to rewritte my URL, and access to test page ( test.php ) , in the url the real path is add, so, I have a 404 error.
http://mywebsite.fr/home/web/dev/test/ <- /home/web/ is the real path
and it can't add to the URL.
In my configuration, I have writting this :
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName *
ServerAlias *
DocumentRoot /home/web
<Directory />
Options FollowSymLinks
AllowOverride None
DirectoryIndex index.php
</Directory>
<Directory /home/web/>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
DirectoryIndex index.php
</Directory>
In my htaccess :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

If you want to add extensions to your URLs like .php or .html then remove those shown rules from .htaccess and add this line:
Options +MultiViews
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.

Related

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]

Remove .php extension in .htaccess not working

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.

CodeIgniter: Removing index.php from the URL

I was trying to remove the index.php from the URL, on my WAMP server THIS worked perfectly, but for some reason on my actual hosted server it's not.
I just created on my current Linux Ubuntu Server a sub-domain,
under /var/www/tools.example.com/public_html
I checked if mod_rewrite is enabled using a .php file I uploaded to the dir with this:
<?php
phpinfo();
and mod_rewrite is under "Loaded Modules".
so no worries here.
the .conf file I created in etc/apache2/sites-available/tools.example.com.conf looks like this:
<VirtualHost *:80>
ServerName tools.example.com
ServerAdmin walid#example.com
ErrorLog /var/www/tools.example.com/logs/error.log
CustomLog /var/www/tools.example.com/logs/access.log combined
DocumentRoot /var/www/tools.example.com/public_html
<Directory /var/www/tools.example.com.conf/public_html/>
Options Indexes FollowSymLinks MultiViews
# changed from None to FileInfo
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
where AllowOverride All so using mod_rewrite with .htaccess is enabled.
I don't know why this is still not working. Any idea why?
the content of the .htaccess file in the main dir:
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Edit #1:
I already went through all first 9 solutions in this post: CodeIgniter removing index.php from url
Your path is incorrect in your .conf file for the directory directive. Your document root listed should be the same as your Directory directive. So I don't think it's picking up the AllowOverride All directive.
Change this
<Directory /var/www/tools.example.com.conf/public_html/>
To this
<Directory /var/www/tools.example.com/public_html/>

How to rewrite directive on apache for anchor cms

I'm using a cms called anchor.
http://anchorcms.com/docs/getting-started/configuration
when I go to domain.com/posts
I get a 404,
when I go to domain.com/index.php/posts
page is displayed correctly.
this is my httpd.conf file
<VirtualHost *:443>
#ssl blah blah
DocumentRoot /var/www/anchor/
ServerName domain.com
ServerAlias domain.com
<Directory /var/www/anchor/anchor/>
AllowOverride All
Options Includes MultiViews
Require all granted
</Directory>
</VirtualHost>
this is my .htaccess file placed inside /var/www/anchor/
Options -indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
I'm a little confused as how to configure this cms, the .htcaccess supposed to go in the "document root", which I'm fairly sure I put in the correct folder. I don't think I have to set the url to a sub directory. any tips?
here's my config file. /var/www/anchor/anchor/config/app.php
<?php
return array(
'url' => '/',
'index' => '',
Your document root is /var/www/anchor, but you only allow overrides on /var/www/anchor/anchor. If you're not globally allowing overrides, that would disable your htaccess file. Try changing your <Directory> tag to match the document root:
<Directory /var/www/anchor>
AllowOverride All
Options Includes MultiViews
Require all granted
</Directory>

Apache's Alias directive is not working as expected

I am trying to integrate my portal with my website.
My website :
http://example.com
and My portal :
http://portal.com
Now I want to see my portal from :
http://example.com/portal
Part of my core apache config file (sites-enabled/website):
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName example.com
DocumentRoot /home/example/WebSite2.0/WebContent
DirectoryIndex index.php index.html
<Directory /home/example/WebSite2.0/WebContent>
Options +IncludesNOEXEC
AllowOverride None
Order allow,deny
allow from all
XBitHack On
AddType text/html .html
AddHandler server-parsed .html
</Directory>
Alias /portal /home/example/portal/CodeIgniter_2.1.0
<Directory /home/example/portal/CodeIgniter_2.1.0>
DirectoryIndex "index.php"
allow from all
Options +Indexes
#Options FollowSymLinks MultiViews
Order allow,deny
RewriteEngine On
RewriteBase /portal
#RewriteRule ^test\.html$ test.php
RewriteCond $1 !^(index\.php|css|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond $1 ^(css|images|js)
RewriteRule ^(.*)$ $1
</Directory>
</VirtualHost>
As you see my portal functions on top of CodeIgniter; Hence -
RewriteCond $1 !^(index\.php|css|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Part of my core apache config file (sites-enabled/portal) :
<VirtualHost *:443>
ServerAdmin webmaster#localhost
ServerName portal.com
ServerAlias www.portal.com
DocumentRoot /home/example/portal/CodeIgniter_2.1.0
DirectoryIndex "index.php"
SSLEngine On
SSLCertificateFile "ssl/portal.com.crt"
SSLCertificateKeyFile "ssl/portal.com.key"
<Directory /home/example/portal/CodeIgniter_2.1.0>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Header unset Server
ServerSignature Off
</Directory>
</VirtualHost>
Now the real problem is when I open http://example.com/portal the browser is looking for the images in the DocumentRoot and not in Alias.
e.g. for image from portal,
<img src="/images/example.png" style="margin-left:30px;height:50px;">
apache error log says -
File does not exist: /home/example/WebSite2.0/WebContent/images/example.png
I would hate to make changes to my code. I just want to get this thing working from the apache config file itself. Please help me do this.
RewriteBase /portal require that the url should begin with /portal. So:
RewriteCond $1 !^(index\.php|css|images|robots\.txt)
will not be hit.
<img src="/images/example.png" style="margin-left:30px;height:50px;">
will try to search file from DocumentRoot.
update1
For there is RewriteBase /portal, example.com/portal/images will hit the Rewrite rule, but example.com/images will not, so:
<img src="/images/example.png" style="margin-left:30px;height:50px;">
should be:
<img src="/portal/images/example.png" style="margin-left:30px;height:50px;">
update2
It is the answer given by #Hussain Tamboli himself, with:
RewriteRule /(images|js|css)/(.+)\.(.+)$ /portal/$1/$2.$3 [PT].
/images/Invoice.png will rewrite to /portal/images/Invoice.png

Categories