I have Urls which will always have http://www.example.com/global/ but after /global/ there may be name of a city or franchise in city or some other page
Examples are
http://www.example.com/global/name-of-city
or
http://www.example.com/global/frnachise-then-name-of-city
Now I have written some rules for both of the conditions & they are working
This is what I'm doing for urls like http://www.example.com/global/name-of-city
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ products.php?city=$1 [L,QSA]
And here is what I'm doing for franchise including urls
RewriteRule ^franchise-management-.([\w-]+)/?$ franchise.php?city=$1 [L,QSA]
but the issue is the 2nd one works only if the 1st RewriteRule is commented otherwise 1st will be applied always
I need to put some condition with a check if url includes franchise then execute RewriteRule for franchise only otherwise execute the 1st RewriteRule.
Try :
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^franchise-management/([\w-]+)/?$ franchise-management.php?city=$1 [L,QSA]
RewriteRule ^([\w-]+)/?$ products.php?city=$1 [L,QSA]
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]
I'm wanting to change directory /rooms to show the name /gear for SEF without having to go through and change the directory name in our script. Note: the directory "gear" does not exist.
For example I want to change:
site/private/rooms/new
to say
site/private/gear/new
My current htaccess says:
RewriteEngine ON
RewriteCond %{THE_REQUEST} \ /(.*)/rooms([^\ ]+)? [NC]
RewriteRule ^ /%1/gear%2 [L,R]
RewriteRule ^(.*)/gear(/.*)?$ /$1/rooms$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]
And it takes me exactly to the directory that I want site.com/gear/new
However, I get a 404 error. I believe because there is no content there? I even tried duplicating folder /rooms and renaming it /gear and I still get a 404.
When the code is:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]
#RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
It takes you to site/private/rooms/new and it displays the content with showing /index.php
Do you think this could be what's creating the problem? The reason why it won't show site.com/gear/new might be because it doesn't display the /index.php?
Try this
RewriteEngine ON
RewriteCond %{THE_REQUEST} \ /(.*)/rooms([^\ ]+)? [NC]
RewriteRule ^ /%1/gear%2 [L,R]
RewriteRule ^(.*)/gear(/.*)?$ /$1/rooms$2 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]
Your rules don't match because
RewriteRule ^/rooms(.*)?$ /gear$1 [R]
The path never contains a leading slash (when using a RewriteRule in an .htaccess). Hence /rooms would never match.
Your rules ignore that the URL path starts with private. They match on rooms and gear as the first path directory instead.
I want to redirect my website to admin panel
Like
localhost/website/ => localhost/website/admin
localhost/website/login => localhost/website/admin
localhost/website/blog => localhost/website/admin
Here is my created rule
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /website/admin/$1 [L]
But this is not working
The following code will redirect everything to your admin page, keeping the attributes. This also applies to files who already exist !
RewriteEngine On
RewriteCond %{REQUEST_URI} !^website/admin
#RewriteCond %{REQUEST_FILENAME} !-f #OPTIONAL: Remove '#' if you want existing files to display normally
#RewriteCond %{REQUEST_FILENAME} !-d #OPTIONAL: Remove '#' if you want existing files to display normally
RewriteRule ^(.*)$ /website/admin/ [L,QSA]
Explanation:
turn on the rewrite engine:
RewriteEngine On
specific condition for rewriting: uri (the part after your domain name/localhost) cannot (! = inverse, NOT) start (^ = start of line) with website/admin = where you are rewriting to. This must be done to prevent looping.
RewriteCond %{REQUEST_URI} !^website/admin
Following 2 lines can be uncommented (remove the #) if you want that existing files are not rewritten
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
The rewrite himself:
^ = start of line
() = a substring
.* = a number of arbitrary symbols, or no symbols
$ = end of line
/website/admin = the place to rewrite to
[L,QSA] = extra options:
L=last: stop executing further lines
QSA=Query String Append: keep arguments, e.g. ?page=contact e.g.
RewriteRule ^(.*)$ /website/admin/ [L,QSA]
I hope this clarifies my solution.
Try:
RewriteEngine On
RewriteCond $1 !^website/admin
RewriteRule ^(.*)$ /website/admin [L]
You can try this with some updated as per your code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/index\.php/component/quates [NC]
RewriteRule .* index.php/component/quotes [R=301,L]
Or Read this link:
http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
This should work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(website/admin)$ [NC]
RewriteRule ^(.*)$ website/admin [R=301,L]
Explanation:
First we make sure that the request is not for a file or directory that exists:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Then we see if website/admin is exactly our requested URL (case-insensitive [NC]):
^ indicates beginning, and $ end of expression.
RewriteCond %{REQUEST_URI} !^(website/admin)$ [NC]
If all conditions are met we redirect to website/admin with HTTP status code 301 (last step [L]):
RewriteRule ^(.*)$ website/admin [R=301,L]
Try this:
RewriteEngine On
RewriteRule ^website/(?!admin).*$ website/admin [L]
RewriteRule ^website/admin[^/]+.*$ website/admin [L]
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
I'm newbie in htaccess..
I have some conditions like this..
if user access
first/second/third
it's equal to
first.api.php?rquest=second&attr=third
but if the value of rquest is 'index', it will be erased from URL, like
first/third
equal to
first.api.php?rquest=index&attr=third
not
first/index/third
I had all night looking for solution, but no result.
I know system being confused if the request is index but attr has a value, it will treat the attr like rquest. see above, first/third, system will treat the 'third' segment as rquest.
Is it possible to rewrite url like that?
this is my work all night, I know this is still crap..
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s
RewriteCond %{QUERY_STRING} !rquest=index
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ $1.api.php?rquest=$2&attr=$3 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/(.*)$ $1.api.php?rquest=$2 [QSA]
RewriteCond %{REQUEST_FILENAME} -s
RewriteCond %{QUERY_STRING} rquest=index
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ $1.api.php?rquest=index&attr=$2 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)$ $1.api.php?rquest=index [QSA,L]
</IfModule>
Try something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ /$1.api.php?rquest=$2&attr=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ /$1.api.php?rquest=index&attr=$2 [L,QSA]
Not clear what you were trying to achieve with your old rewrite rules, but the above should do what you've described.