Symfony2: simple rewrite rule in vhost to strip .html extention out - apache

Since I migrated my project on Symfony2, I also avoided to use .html extensions previously used for my generated urls.
So, after looking at some examples, I'am trying to execute this in my /etc/apache2/sites-available/mysite:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.html$ $1 [NC,L]
#this is the standard rewrite rule to redirect every request to /web/app.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
I have tried many combinations but no one seems to work. Any Help? Thanks

Related

htaccess not rules with index and another page with param

I have a problem to configure a simple htaccess with two pages to rewrite: the index and the page.php?param=valueparam
The problem is that one of these rules its correct the another don't work.
These is that I need:
index.php -> mydomain.com/
page.php?param=valueparam -> mydomain.com/valueparam
I tried several options but anything rules that I want:
Options +FollowSymLinks
Options -Indexes
php_value default_charset None
AddDefaultCharset utf-8
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-z_-]*)$ page.php?param=$1 [NC,L]
Based on your shown samples only, could you please try following htaccess rules file, please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
##For index.php -> mydomain.com as per OP's first condition.
RewriteRule ^/?$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(valueparam)/?$ page.php?param=$1 [NC,L]

.htaccess URL rewrite not working, tried all possible options

I was trying to rewrite a URL for making my site SEO friendly, but .htaccess rewrite not seems to work.
My URL is
www.tasteofkochi.com/dine-detail.php?a=150
I need this to look like
www.tasteofkochi.com/sometext/150
I did the simple formula but it's not reflecting, intact nothing happens at all. If I add some junk char in htaccess, site crashes, which means htaccess is working fine. Also I added a formula to remove .php extn, and that too works fine. Only issue is with this one. Can anyone please help me. I enable rewrite in httpd and allow all in directories, still not working.
Below is my .htacces
RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /
## hide .php extension
# To externally redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule ^detail/([0-9]+)/?$ dine-detail.php?a=$1 [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?p=$1 [L,QSA]
We can create pretty urls with .htaccess and php by mainly two files one is .htaccess and another index.php
Example

How can I use mod_rewrite on current directory only?

Currently I have these rules in my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*).css style.php?u=$1 [QSA]
RewriteRule ^(.*).xml rss.php?u=$1 [QSA]
</IfModule>
This will rewrite the following URLs:
http://domain.com/user.css
http://domain.com/user.xml
But when I'm trying to grab a file from a subdirectory: http://domain.com/css/style.css it gets rewritten as well.
My goal is rewrite only for current directory and avoid sub-directories, since all real CSS files on sub-directories will be rewritten.
How I can avoid this?
You need to make your pattern more restrictive: this ^(.*).css will match ANYTHING with .css in it while this pattern ^([^/]+)\.css$ will be restricted to something.css (styles\something.css will not match it).
<IfModule mod_rewrite.c>
RewriteEngine On
# do not do anything to real files or folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
RewriteRule ^([^/]+)\.css$ style.php?u=$1 [QSA,L]
RewriteRule ^([^/]+)\.xml$ rss.php?u=$1 [QSA,L]
</IfModule>
The easiest way is to tell mod_rewrite to avoid rewriting if the file is a real file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*).css style.php?u=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*).xml rss.php?u=$1 [L,QSA]
I've added a [L] tag (end) because once a rule is applied you certainly doesn't need the next rule to be checked.
Now if the files really exists but you really want to handle them with a php script if no 'css' subdirectory is present on the url... let's try that:
<Location "/css">
RewriteEngine off
</Location>

Allow URLs without any extension with url rewriting, like exemple.com/forum

I need to allow urls like http://example.com/forum on my website.
Here is my .htaccess :
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([a-z-]+)$ /$1.php [L]
Currently, I get a 404 error.
If I change ^([a-z-]+)$ /$1.php [L] to ^([a-z-]+).html$ /$1.php [L], and my url http://example.com/forum.html, it works.
I want something like stackoverflow, http://stackoverflow.com/questions
EDIT: It works with WAMP in localhost, but on the website, it doesn't.
What's missing?
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# add .php file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule (.+) /$1.php [L,QSA]
I have added Options -MultiViews -- can make a big difference on some configurations (possibly your case).
Rule is now also checking if such .php file actually exist. If not -- no rewrite occurs.

Folder control with .htaccess

I have a directory /public_html/myfolder/ and I want my domain www.example.com to point to /public_html/myfolder/ as my root folder.
I figured that out, but problem is, my images are not showing.
Here's my .htaccess file:
Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .* https://www.example.com/$1 [L,R=301]
RewriteRule ^$ myfolder/index.php [L]
# Require SSL
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST} [L]
# Rewrite rules
<IfModule mod_rewrite.c>
#RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ myfolder/index.php?q=$1 [L,QSA]
</IfModule>
The issue may be that you have relative paths specified for your images on pages and after the URL is rewritten the links get broken. To fix this cosider using root-relative paths for the resources.
You will need to make sure the images are ignored by using RewriteCond's.
Try something like
RewriteCond %{REQUEST_FILENAME} !\.(jpg|png|gif)$
(not sure if the . should be escaped, but i think it should be)
You can also try adding
RewriteLog
RewriteLogLevel 3 (or maybe greater)
I'm not sure if this works if it is inserted into a .htaccess, it might be that it has to be in the server conf, but it is worth a try. It is easier to debug rewrite if you have the log.