Multiple Rewrite Rules with variables in .htaccess - php

I am trying to use htaccess Rewrite Rules. I need
http://www.sitename.com/variable/upload.php
to map to
http://sitename.com/upload.php?slug=variable
http://www.sitename.com/variable/gallery.php
to map to
http://sitename.com/gallery.php?slug=variable
http://www.sitename.com/variable/
to map to
http://sitename.com/home.php?slug=variable
For the third part I have:
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) home.php?slug=$1 [L]
But Now http://www.sitename.com/variable/upload.php also map to http://sitename.com/home.php?slug=variable
How can i do this ?

Have your rules like this:
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ $2?slug=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ home.php?slug=$1 [L,QSA]
i.e. handle 2 slashes rule before you rewrite to home.php

Related

Creating .htaccess hidden redirect

How can I make redirect from /katalog/protein/api and /katalog/protein/activelab to katalog/protein/?ms|manufacturer=API and katalog/protein/?ms|manufacturer=ActiveLab.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/katalog/(protein|gainer)/(api|activelab)/?$ /katalog/$1/?ms|manufacturer=$2 [L,QSA]
Your code look fine, just remove the leading slash from RewriteRule's pattern.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^katalog/(protein|gainer)/(api|activelab)/?$ /katalog/$1/?ms|manufacturer=$2 [L,QSA,NE,NC]

mod rewrite: how to check if url exists else redirect path

I am quite new to mod rewrite and i have this, which is not working.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /kitnes/cache/dynamic/$1.htm
RewriteRule /kitnes/cache/dynamic/([^/\.]+).htm index.php?id=$1 [NC,QSA,L]
the rewritten URL is something like this http://www.ghananewsportal.com/1.1629334 and i would like it to redirect to http://www.ghananewsportal.com/?id=1.1629334 if the rewritten URL fails.
thanks.
Something like this should work:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/kitnes/cache/dynamic/$1.html -f
RewriteRule ^(.+)$ /kitnes/cache/dynamic/$1.htm [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?id=$1 [QSA,L]

htaccess rewrite php to html, no post method?

I use this htaccess to rewrite php extensions to .html.
I have a valid form which should be posting to an url, but I can't get the post data, I think this has something to do with the htaccess.
Options +FollowSymlinks
Options -Indexes
RewriteEngine On
RewriteBase /mypath/
# Rewrite to SEF URL's
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)/(.*)\.html index.php?a=$1&b=$2&c=$3&d=$4 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)\.html index.php?a=$1&b=$2&c=$3 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)\.html index.php?a=$1&b=$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html index.php?a=$1 [QSA,L]
Does someone has a solution to solve my problem?
I guess, you must use a non-greedy regular expression. Replace .* with .*?
RewriteRule ^(.*?)/(.*?)/(.*?)/(.*?)\.html index.php?a=$1&b=$2&c=$3&d=$4 [QSA,L]
and the others appropriately.

mod_rewrite not recognizing files

My mod_rewrite is working excellent, but it's not recognizing anything after for example /calgary/
So, if I go to /calgary/login.php and it is only seeing the index.php page? It won't recognize the /login.php page?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /city_name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/?$ /city_name/index.php?page=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /([^\./]+)\.php$
RewriteCond %{DOCUMENT_ROOT}/city_name/%1.php -f
RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/?$2.php?page=$1 [L,QSA]
</IfModule>
You need to swap those 2 rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /city_name
# This used to be the 2nd rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /([^\./]+)\.php$
RewriteCond %{DOCUMENT_ROOT}/city_name/%1.php -f
# there used to be a "?" here, remove it ----v
RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/$2.php?page=$1 [L,QSA]
# this used to be the first rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/?$ /city_name/index.php?page=$1 [L,QSA]
</IfModule>
The less restrictive rule (the one that matches (.*)/?$) will match /calgary/login.php outright before the second set of rules gets to do its thing, which seems to be rewritten to /city_name/?login.php?page=calgary.
Is that really what you want? There are two ? in the target there. Maybe you only want /city_name/$2.php?page=$1?
Try removing the 'L' from the first rewrite rule, this tells Apache to stop processing further rules if a match is found.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /city_name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/?$ /city_name/index.php?page=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /([^\./]+)\.php$
RewriteCond %{DOCUMENT_ROOT}/city_name/%1.php -f
RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/?$2.php?page=$1 [L,QSA]
</IfModule>

Why my htaccess rules do not work perfectly?

I want to make 2 types of URL with these styles using htaccess and mod_rewrite.
http://www.site.com/product
http://www.site.com/product/1/something
For this, I've added these lines to the htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?p=$1&id=$2&des=$3 [N]
RewriteRule ^([^/]*)$ /index.php?p=$1 [F]
But whenever I run my application and hint http://localhost/project/, web server redirect me to http://localhost/.
Now I have 2 questions:
Why do I redirect to localhost and why the codes above do not work ?
How can I add a trailing slash to the end of clean URLs?
Adding
RewriteBase /
should help
Change the rules to
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /index.php?p=$1&id=$2&des=$3 [N]
RewriteRule ^([^/]*)/?$ /index.php?p=$1 [F]
The final .htaccess should look like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /index.php?p=$1&id=$2&des=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /index.php?p=$1 [L]
You probably want to get rid of the N and F flags. Not sure why you are getting redirected, it doesn't look like the rules you have are responsible for that. You also want to repeat the 2 conditions you have for both rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?p=$1&id=$2&des=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?p=$1 [L,QSA]

Categories