Alright. Here is my htaccess code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ bank.php?bank=$1
#This will make my url e.g. site.com/citibank
RewriteRule ^(.*)/(.*)$ state.php?bank=$1&state=$2
#This will make my url e.g. site.com/citibank/new-york
RewriteRule ^(.*)/(.*)/(.*)$ location.php?bank=$1&state=$2&district=$3
#This will make my url e.g. site.com/citibank/midtown
PROBLEM: Its working till second rule but when it comes to third and final rule, it created url like: site.com/bank.php/citibank/new-york
Any help would be appreciated.
Thanks
It is because your first rule is using .* pattern and is matching everything. Change your code to this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ bank.php?bank=$1 [L,QSA]
#This will make my url e.g. site.com/citibank
RewriteRule ^([^/]+)/([^/]+)/?$ state.php?bank=$1&state=$2 [L,QSA]
#This will make my url e.g. site.com/citibank/new-york
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ location.php?bank=$1&state=$2&district=$3 [L,QSA]
#This will make my url e.g. site.com/citibank/midtown
Related
I'm trying to do a rewrite, where if the page doesnt exist, it rewrites to a category page. Its a bit different to the normal RewriteCond %{REQUEST_FILENAME} -f as the page name is dynamic based on the URL.
If somebody visits a dynamic path name such as "/contacts"
I first want it to check if the following folder/file exists based on the path name:
/[dynamicpath]/[dynamicpath].php
example:
/contacts/contacts.php
If that doesnt exist, rewrite to the following page
/categories/category/category.php?cat_url=[dynamicpath]
example:
/categories/category/category.php?cat_url=contacts
I've tried the below, but it fails with the first RewriteRule triggering when the file exists and goes straight to the category rewrite.
RewriteCond %{SCRIPT_URL} ^(.+)
RewriteCond %{DOCUMENT_ROOT}/%1/%1.php !-f
RewriteRule (.+)/?$ $1/$1.php [QSA,L]
RewriteRule ^([\w-]+)/?$ /categories/category/category.php?cat_url=$1 [QSA,L]
You may use these rules in your site root .htaccess:
RewriteEngine On
# attempt rewrite to /[dynamicpath]/[dynamicpath].php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1/$1.php -f
RewriteRule ^(.+?)/?$ $1/$1.php [L]
# else rewrite to categories/category/category.php?cat_url=...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ categories/category/category.php?cat_url=$1 [QSA,L]
I found the answer by checking the server variables being passed and discovered %{REQUEST_FILENAME} included the full file path (document_root+filename).
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}%{REQUEST_URI}.php -f
RewriteRule ^(.+)/?$ $1/$1.php [L]
RewriteRule ^(.+)/?$ categories/category/category.php?cat_url=%{REQUEST_URI} [QSA,L]
my URL is of the type: www.mysite.fr and for any page: www.mysite.fr/index.php/test
I wish that www.mysite.fr/test displays www.mysite.fr/index.php/test (it's more beautiful without index.php!)
I tried this but: www.mysite.fr/test displays the home page instead of the test page
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/(.*)index\.php/(.*)\sHTTP.*$ [NC]
RewriteRule ^ /%1%2 [R=301,L]
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^(.*)$ index.php/$1 [L]
Second line to remove index.php exteranlly.
Fifth line to redirect request to original path internally .
Note: clear browser cache the test
Your method works on my localhost but not on my online site. This is the 5th line that blocks in my opinion but I do not see why even adding a slash before index.
Someone have an idea ?
I have:
RewriteCond %{HTTP_HOST} ^my.domain.com$ [NC]
RewriteRule ^(.*)$ index.php?q=search&keyword=$1
Input:
my.domain.com/foo_bar
I want:
index.php?q=search&keyword=foo_bar
But in fact:
index.php?q=search&keyword=index.php
I don't understand why. Please help me!
Your rewrite rule is actually rewriting twice, once for /foo_bar and second time for index.php as .* matches anything.
You just need to add 2 conditions to stop rewrite for files and directories:
# handle landing page
RewriteCond %{HTTP_HOST} ^my\.domain\.com$ [NC]
RewriteRule ^/?$ index.php?q=search [L,QSA]
# handle /foo_bar
RewriteCond %{HTTP_HOST} ^my\.domain\.com$ [NC]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?q=search&keyword=$1 [L,QSA]
I have this .htaccess which basically is supposed to turn my links from
http://www.dsbigiena.ro/products.php?product-id=Colac-wc-cu-folie-de-plastic-cu-actionare-manuala-prin-buton
to
http://www.dsbigiena.ro/Colac-wc-cu-folie-de-plastic-cu-actionare-manuala-prin-buton/
Unfortunately, it's not doing it.
Did I write it wrong?
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]*)/$ /products.php?product-id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
EDIT: I did indeed test the .htaccess with "deny from all", and it works.
Your current rewrite rule only resolve a pretty URL. It won't convert an actual /products.php URL to the pretty one by itself. You need to have an explicit rule for that as
RewriteEngine On
RewriteBase /
# Redirects from a normal URL to a pretty one
RewriteCond %{THE_REQUEST} \ /products\.php\?product-id=([^\ ]+) [NC]
RewriteRule ^ /%1/? [L,R=301]
# Resolves the pretty URL
RewriteRule ^([^/]+)/?$ /products.php?product-id=$1 [QSA,L]
I would like to have multiple RewriteRules in my .htaccess file.
# Enable Rewriting
RewriteEngine on
# Rewrite profile urls
# Input: /user<userId>
# Output: /profile.php?id=<userId>
RewriteRule ^user(\d+)/?$ profile.php?id=$1 [L]
# Rewrite by default to redirect.php
RewriteRule .* redirect.php
Every requests points to redirect.php
I thought, with the [L] flag in the first RewriteRule, would stop processing the rule set.
If you would like to rewrite /user<userId> to /profile.php?id=<userId> and rewrite the other URLs to /redirect.php, then you could try these two configuration directives:
RewriteEngine on
RewriteRule ^user([a-zA-Z0-9_-]+)/?$ /profile.php?id=$1 [L]
RewriteRule .* /redirect.php
OR:
RewriteEngine on
RewriteRule ^user([a-zA-Z0-9_-]+)/?$ /profile.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /redirect.php
Adding the following to the second RewriteRule works.
# Rewrite by default to redirect.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* redirect.php
But im not sure, why this is needed.
[L] flag stops processing only in current iteration but mod_rewrite repeat process until path will not be constant.
In your case I think you can use this rule:
RewriteRule ^user\d+$ profile.php
RewriteCond %{REQUEST_URI} !profile.php
RewriteRule ^.*$ redirect.php