introducing .htaccess file in PHP MVC Project - php

I've recently started working on mvc. My problem is the recognition of css files.
When I added the line
RewriteRule ^([^\/]*)/([^\/]*)$ index.php?page=$1&id=$2 [L,QSA]
to the .htaccess file, another css file does not know. but when change it to
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
it works fine.
here is my .htaccess file detail
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(css|pdf|gif|ico|jpg|js|png|swf|pdf|txt|php)$
RewriteRule ^([^\/]*)/([^\/]*)$ index.php?page=$1&id=$2 [L,QSA]
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
what can i do to solve the problem

try this..
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>

Related

.htaccess rewrite options for index.php and GET variables

I have a problem with the Rewrite options and I can't find the right answers on the internet so I hope this community can help me.
I am working local on a web project and use XAMPP as my local webserver.
I made a VirtulHost (basic.localhost) and now there is my problem with the mod_rewrite options.
When there is an url: basic.localhost/index.php/var1=option1&var2=option2&var3=option3&var4=option4
I want to rewrite the url to this: basic.localhost/option/option2/option3/option4
The var1 to var4 variables are used as GET variables to select content from an database.
This is my .htaccess now:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
And all Rewrite Rules that I tried does not work...
I found an answer for the problem with the variables:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2&var3=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2 [L,QSA]
RewriteRule ^([^/]+)$ index.php?var1=$1 [L,QSA]
</IfModule>
Now there is only the Problem with the index.php.
I finally got a solution for this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2&var3=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2 [L,QSA]
RewriteRule ^([^/]+)$ index.php?var1=$1 [L,QSA]
</IfModule>

RewriteCond exclude folder

I have the following in my .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [L]
This works nicely, but I don't want the RewriteRule to trigger for the following folder and its subfolders.
www.site.co.uk/Content
How do I do this?
Try this Rule,
RewriteEngine on
RewriteRule ^(content)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [L]
or create a .htaccess in content directory and put
RewriteEngine off

PHP Pretty URLs combination of two mod_rewrite rules?

I have this .htaccess code in the root folder to replace this url: localhost/mysite/profile.php?username="someone" to become localhost/mysite/someone.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mysite/profile.php?username=$1
This is working fine but what I want is to include subpages like for example the about page to be localhost/mysite/about instead of localhost/mysite/about.php. And the subpages are in the root folder too.
How can I achieve these two rewrites?
You can have this code in mysite/.htaccess:
RewriteEngine On
RewriteBase /mysite/
# add .php internally to files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# handles profile URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [L,QSA]
Try below code it's working fine to me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try This
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mysite/([A-Za-z0-9-]+)$ /mysite/profile.php?username=$1

Rewrite url to site.php as site

Here is my files
index.php
blog.php
about.php
I am accessing it as
localhost/index.php or localhost
localhost/blog.php
localhost/about.php
How can i rewrite .htaccess so that i can access it by
localhost/
localhost/blog
localhost/about
Update
My image are not displaying once i applied the below provided .htaccess, can any give fix this ?
Simple try this in .htaccess file :
RewriteRule ^([a-zA-Z0-9_-]+)$ $1.html
Options -MultiViews # optional Depends on Server
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
EDIT:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
In your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteRule URL

I am having problems the .htaccess file using the RewriteRule. this is how my directory is right now example.com/pages/contact.php I want my directory to look like this example.com/contact/ Is there a way using the rewriterule to get hide the .php and the sub directory /pages/
This is the code i have right now:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^contact/$ /pages/contact.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
</IfModule>

Categories