htaccess rewrite condition not working - php

I am working on URL of my website. I have just rewrite the URL From
http://www.website.com/index.php?url=some-text
to
http://www.website.com/some-text
For this I'm using following code of .htaccess
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
Now, I want to add another variable page number in URL.
http://www.website.com/index.php?url=some-text&page=1
I want to rewrite it like
http://www.website.com/some-text/1/
AND
http://www.website.com/some-text/1
I have tried following code but some reason it is not working, it showing me server error.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)/(.*)$ index.php?url=$1&page=$2 [L,QSA]
RewriteRule ^(.*)/(.*)/$ index.php?url=$1&page=$2 [L,QSA]

You don't need 2 new rules. Just one with optional / will be enough. Your complete code:
Options +SymLinksIfOwnerMatch -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$1&page=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ index.php?url=$1 [L,QSA]
Another problem in your new code is that RewriteCond are not getting applied for last RewriteRule since these are applicable to very next RewriteRule only.

Related

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

htaccess rewrite rule for subfolder (nice url)

I have trying to figure out how to get working in subfolder "admin" the same thing which works in root dir, problem is I don't know how to do that.
Currently I have:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]
# /
RewriteCond %{REQUEST_URI} !\.[[:alnum:]]+$
RewriteRule ^(.+[^/])$ /$1/ [R=301]
Now it works like:
http://example.com/page translate into http://example.com/?arg=page
What I would like to do in addition is this:
http://example.com/admin/page translate into http://example.com/admin/?arg=page
I don't know at all how to make that happen, can somebody help me please?
Thanks
You can use these rules with appropriate RewriteBase:
RewriteEngine On
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]
Or just add
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin/(.*)$ admin/index.php?arg=$1 [L,QSA]

Access to my sub directories using htaccess

I'm using htaccess to rewrite my url first one is working fine now
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ /oneday/index.php?do=$1 [L,QSA]
to rewrite url but now I want to access my sub directories and my directories exists like this Oneday/files/pdf-generation/dock-receipt-PDF24032015.pdf and for this my code is
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\?*$ /oneday/files/pdf-generation/path$1
and my complete code of htaccess is
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ /oneday/index.php?do=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\?*$ /oneday/files/pdf-generation/path$1
but the second one is not working for me. How could I do this and Oneday is my project directory name.
Now first url create this link http://localhost/oneday/bookings and now I want to access my PDF files http://localhost/oneday/files/pdf-generation/dock-receipt-PDF03242015.pdf
You can target 2nd rule only for existing files:
RewriteEngine On
RewriteBase /Oneday/
RewriteCond %{DOCUMENT_ROOT}/Oneday/files/pdf-generation/path/$1 -f [NC]
RewriteRule ^(.+)$ files/pdf-generation/path/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?do=$1 [L,QSA]

.htaccess redirection based on number of slashes(/) in url

This is my first htaccess file.
this is what i have written
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(css|js|png|jpg|gif|otf|eot|svg|ttf|woff|jpeg)$ - [L]
RewriteRule ^([^/]*)/([^/]*)$ maincategory/index.php?main=$1&sub=$2 [L,QSA,NC]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ subcategory/index.php?main=$1&sub=$2&cat=$3 [L,QSA,NC]
The problem what I am facing is both are pointing to maincategory.
How can I redirect based on number of "/" in the URL?
Thanks in advance.
Rearrange the rules and try it this way. Let me know if that works for you.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(css|js|png|jpg|gif|otf|eot|svg|ttf|woff|jpeg)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ subcategory/index.php?main=$1&sub=$2&cat=$3 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ maincategory/index.php?main=$1&sub=$2 [L,QSA,NC]

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>

Categories