.htaccess RewriteRule with file extension - php

This question might be a duplicate but i'm trying to do a URL Rewrite by converting :
http://example.com/file.php?f=the_file_name&ext=the_file_extension
to
http://example.com/video2.mp4
I tried the following RewriteRule script but it change also others folders :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+).([^/]+)$ file.php?f=$1&t=$2 [L]
I couldn't get them work.. What am I doing wrong? and how can I make it work?
Thanks !

Dot is special regex symbol and it needs to be escaped:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)\.([^/]+)$ file.php?f=$1&t=$2 [L,QSA]

Related

I am using htaccess that is not working

I have url like
http://example.com/index.php/allmodels/Samsung-mobiles_80
and i want to convert into
http://example.com/index.php/allmodels?st=Samsung-mobiles_80
I am using following code
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^allmodels/(.*)$ /allmodels?st=$1 [NC,L]
but this code is not working.
All the setting is correct I have tested and htaccess is working.
When i used following code that is working but above given code is not working.
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$1 [NC,L]

Rookie Url Rewriting 2

I have this rewrite rule in my htaccess script, but whenever i do a var_dump on $_GET it's empty, like the values from the url are not read in PHP.
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.php/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?app=$1&page=$2&category=$3 [L]
</ifModule>
Example of url: .../index.php/main/featured/1
How can one fix this problem?
Thank you!
I believe these two conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
are not met as your URL is /index.php/main/featured/1. At least it did not work for me either. Can you either remove index.php part both from RewriteRule and your URL, making .htaccess:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?app=$1&page=$2&category=$3 [L]
</ifModule>
and navigating to URL /main/featured/1 or if you really need the index.php part, then uncomment/remove these conditions:
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
and continue using /index.php/main/featured/1 URL.

Yii and .htaccess redirect

I want to redirect pages from my_site.com/gamenews.php?id={ID} to my_site.com/news/{ID}/, but can't :(
My .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/index\.php /$1/index [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
RewriteRule ^gamenews\.php\?id=(.*) /news/$1/ [L,R]
AddDefaultCharset UTF-8
Error that displays:
Unable to process request "gamenews.php"
What I'm doing wrong? Thanks.
Try like this
RewriteRule ^news/([0-9]+)/ gamenews.php?id=$1[L,R]
The rewrited url should go first
[0-9] means that second parameter should be a number
The order of your htaccess rules might be your problem in addition to your syntax. Unless I misunderstood your question.
Try this in your htaccess.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^news/(.*) gamenews.php?id=$1 [L]
RewriteRule ^(.*)/index\.php /$1/index [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
AddDefaultCharset UTF-8

URL doesn't work with slash after removing extension using htaccess

I have been working on localhost, and my htaccess file is
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
after adding the htacces code,the url
localhost/movies/news.php
works
localhost/movies/news
also works but
localhost/movies/news/
doesn't work. It shows "Internal Server Error".How to make it work with slash and without slash.
You an try this code:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Internally forwards movies/news/ to movies/news.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
The problem is when you add the slash you have news/.php and this is not working.
A better solution is to rewrite to a GET variable something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?url=$1 [L]
Then you can filter the GET variable in your script and include the file or content you need.

php mod_rewrite Permalinks

I am looking for a way to access to:
http://myurl.com/?file=random_token
with
http://myurl.com/random_token/
I already read about mod_rewrite in the .htaccess but I don't know how to setup the pattern.
I tried this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule /(.*)/$ /index.php?file=$1
It would be nice if you can help me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/$ /?file=$1 [L]
Try this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCOnd %{REQUEST_FILENAME} !-d
RewriteRule /?([a-z-]+) index.php?file=$1 [L,QSA]

Categories