Is it possible to exclude the .php from URL?
Currently i got this in my .htaccess to remove php from urls:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But whenever i try to do
dashboard?test=lol
It doesn't work, but if i do
dashboard.php?test=lol
then it does work.
I've seen other sites being able to do it without the .php in url... so how?
Thanks.
Have you restarted the webserver?
You also need to enable AllowOverride in Apache configuration. Something similar to this:
AllowOverride All
With your shown samples, try following. Please make sure to clear your browser cache before testing of URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?(\S+)\s [NC]
RewriteRule ^ /%1?%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ $1.php [QSA,L]
Just add this line in your site root .htaccess to enable content negotiation:
Options +MultiViews
Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.
Related
I'm using the following .htaccess file in order to internally rewrite URLs without the file extension, i.e. /mypage opens the content of /mypage.php or /mypage.html
Options -MultiViews
# start rewrite engine
RewriteEngine on
# internally rewrite "page" to "page.html"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
# internally rewrite "page" to "page.php"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
It works exactly is it should on my local WAMP server.
However on my shared hosting service it gives an Internal server Error (500).
My hosting provider told me that the error log says that "Option MultiViews not allowed here" and confirmed me that MultiViews is enabled by default.
If I comment the first line there is no Error 500 but the rewriting doesn't work anymore (because MultiViews is enabled). They said to me that they can not change it for my host.
So is it possible for me to get this .htaccess rewriting URL working with MultiViews on ?
The following .htaccess was able to allow my symfony project domain to be served without app.php in the URL, as intended. The only issue is that it is breaking all other (non-symfony related) urls and causing an internal server error.
Right now, after deleting the .htaccess file from the server, all my domains work but the project tied to symfony must be accessed using app.php in URL ?
Is it possible to modify the below .htaccess to rewrite the symfony url to not require app.php in the URL while still allowing for all other URLs, not tied to symfony, to be accessed successfully?
Not sure this is needed, but assume the domain tied to symfony is www.apples.com.
Thanks in advance!
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Explicitly disable rewriting for front controllers
RewriteRule ^app.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
# Change below before deploying to production
RewriteRule ^(.*)$ /app.php [QSA,L]
</IfModule>
Edit
I also tried the following:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Explicitly disable rewriting for front controllers
RewriteRule ^app.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change below before deploying to production
RewriteRule ^(.*)$ app.php [QSA,L]
DirectoryIndex index.php
</IfModule>
Right after
RewriteCond %{REQUEST_FILENAME} !-f
add
RewriteCond %{REQUEST_FILENAME} !-d
and set DirectoryIndex to index.php
A bit late to this game but I just found an Apache directive that saves so much time and makes your htaccess cleaner. The only caveat is you must have Apache 2.2.16+. To have all urls except valid files (images, etc) use app.php as a front controller use the FallbackResource directive.
<Directory "/web/symfony-project">
FallbackResource /app.php
</Directory>
I've been looking for my answer for 2 days but since I haven't found it, I finally decided to put my question to you.
I'm using a Brinkster shared host and can access the .htaccess but no Apache configuration. Every folder on the server represents the content of one site (domain). In the root I have a .htaccess where every domain is controled with
RewriteCond %{HTTP:Host} ^(?:www\.)?domainname\.be$
RewriteRule (.*) /folder/$1 [NC,L,NS]
In the folder I have another .htacces file.
I want to rewrite and redirect for prettier URLs
http://www.domainname.com/pag.asp?name=var_name&id=var_id
should look like
http://www.domainname.com/name/
The rewrite bit works with the .htaccess code bellow.
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks -MultiViews
Options FollowSymLinks
AllowOverride All
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^name=([a-zA-Z-]+)&id=([0-9]+)$ [NC]
RewriteRule ^/?page\.asp$ /%1? [R=301,L]
</IfModule>
But for some reason www.domainname.com/name/ always gives a 404.
What am I doing wrong?
Any help wouldd be highly appreciated.
Thank you
Initially, this code was written to create "pretty" urls.
When using the code below, mod_rewrite works as it should.
<Directory /var/www/vhosts/myurl.com/httpdocs>
Options Indexes FollowSymLinks
php_admin_flag engine on
php_admin_value open_basedir none
AllowOverride all
Order allow,deny
Allow from all
</Directory>
RewriteEngine on
RewriteCond %{HTTPS} !=off
RewriteCond %{LA-U:REQUEST_FILENAME} !-f
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond $1 ^(register|account|logout|profile|edit_profile).*$
RewriteRule ^/(.*)$ https://myurl.com/?get=$1
It takes any matching url such as https://myurl.com/register and rewrites it as https://myurl.com/?get=register. The appropriate page is found and displayed in the browser.
However, I want the original url to be passed through to the browser. To achieve this, I added the [PT] Flag to my RewriteRule as shown below:
RewriteRule ^/(.*)$ https://myurl.com/?get=$1 [PT]
This lets https://myurl.com/register (same url as above) through to the browser but no longer displays the page. Instead, it returns the following error:
Bad Request
Your browser sent a request that this server could not understand.
Client sent malformed Host header
Relevant Info:
OS: Linux
Server: Apache
Control: Plesk
Directory: /var/www/vhosts/myurl.com/conf
File: vhost_ssl.conf
I've searched multiple forums and articles to no avail.
Article 1: Making prettier URLs with mod_rewrite (includes use of [PT] Flag)
Does anyone have ideas on what's going on here and how to fix it? How can I get the "pretty" url to display?
So after two days of frustration, I finally solved the puzzle.
First I experimented with the RewriteRule from by changing it from:
RewriteRule ^/(.*)$ https://myurl.com/?get=$1 [P]
to
RewriteRule ^/(.*)$ https://myurl.com/index.php?get=$1
without any flags.
This produced an infinite loop whenever the rewrite was triggered. But I noticed that the loop was happening without redirecting the url (maintaining the 'pretty url'). Progress!
I decided to simplify the code. I removed 'LA-U:' from the RewriteCond as shown below:
RewriteCond %{LA-U:REQUEST_FILENAME} !-f
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
to
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
That did the trick! Thanks to those that commented. Hope this helps someone in the future.
I'm trying to implement a REST-style URL with a mod-rewrite turned on in .htaccess. There's a bit of a kicker which is that I'm developing in a test environment (new cpanel account). Here's the .htaccess:
RewriteEngine on
#REMOVE THIS LINE ON SITE LAUNCH!
RewriteBase /~myNewAccount/
#Hide .php extensions for cleaner URLS
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Options All -Indexes
The URL I CAN use looks like this:
www.example.com/~myNewAccount/index.php/id/50
I can access the PATH_INFO here, but when I try to do this:
www.example.com/~myNewAccount/index/id/50
...I get a 500 internal server error. I've tried implementing the solution found here by Gumbo but that mucks things up.
Ideas on what might be causing this?
Try this rule:
RewriteRule ^index(/.*)?$ index.php$1 [L]
Or if you don’t want index to be in the URL path at all:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php/$0 [L]