OR Condition in htaccess URL re-writing - php

I have done a URL re-writing in my .htaccess file for manage_districts.php as follows:
RewriteRule ^manageDistricts/([0-9]+)/?$ manage_districts.php?editid=$1 [NC,L]
RewriteRule ^manageDistricts/([0-9]+)/?$ manage_districts.php?delid=$1 [NC,L]
Here you can see that for both, edit and delete operations I have a same page so, instead of writing the same rule for the same file I want to write a single rule where I can pass the editid as well as delid with an OR Condition.
I have already tried this syntax,
RewriteRule ^manageDistricts/([0-9]+)/?$ manage_districts.php?(editid|delid)=$1 [NC,L]
but it's not working.

You can't use the same URL for two different actions. So, add a second parameter with values either "edit" or "delete".
RewriteRule ^manageDistricts/edit/([0-9]+)/?$ manage_districts.php?editid=$1 [NC,L]
RewriteRule ^manageDistricts/delete/([0-9]+)/?$ manage_districts.php?delid=$1 [NC,L]
And in your html:
Edit
Delete

Related

Writing .htaccess Rewrite rules with similar strings

I am writing rules in my .htaccess file to configure the URL shown.
In my site I have two pages. Eg. page.php and page_edit.php.
I my .htaccess file I am making the following rules:
RewriteRule ^page page.php [NC,L]
RewriteRule ^page/create page_edit.php?editID=1 [NC,L]
However, the 2nd rule never gets caught and the page is always directed to page.php (the first rule). I guess this is becuase it is has the string for the first rule ('page') as a substring.
How can I get round this?
you can change Rule order, the more specific in top
RewriteRule ^page/create page_edit.php?editID=1 [NC,L]
RewriteRule ^page page.php [NC,L]

htaccess 3 different urls in one php file

I have 3 step in one php file:
This is my htacess now:
RewriteRule ^igra/(.*)$ "/index.php?page=igra&id=$1"
RewriteRule ^igra/(.*)/sezona/(.*)$ "/index.php?page=igra&id=$1&season=$2"
RewriteRule ^igra/(.*)/sezona/(.*)/liga/(.*)$ "/index.php?page=igra&id=$1&season=$2&league=$3"
When i go in browser something like index.php?page=igra&id=$1 or index.php?page=igra&id=$1&season=$2 or index.php?page=igra&id=$1&season=$2&league=$3 sure with real values it works fine, but when i try to access with this pretty links it always show me the first rewrite rule..
I hope u understand me what i need here, best regards..
Your first rule is capturing everything, so the subsequent rules never get executed. Just switch them around:
RewriteRule ^igra/(.*)/sezona/(.*)/liga/(.*)$ /index.php?page=igra&id=$1&season=$2&league=$3 [L]
RewriteRule ^igra/(.*)/sezona/(.*)$ /index.php?page=igra&id=$1&season=$2 [L]
RewriteRule ^igra/(.*)$ /index.php?page=igra&id=$1 [L]
Notice also the addition of the L flag.

Except some url from a rewrite rule

I inserted this rule in my .htaccess file:
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?content=category&categoryname=$1 [NC,L]
In this way I can get friendly urls like this:
http://localhost/mysite/london
I'd also like to use a friendly url for my contact page like so:
https://localhost/mysite/index.php?content=message to become:
https://localhost/mysite/contact
But if I insert the below rule into .htaccess...
RewriteRule ^contact/?$ index.php?content=message [NC,L]
...it doesn't work as it seems that the rule for the categories affects this rule.
In fact, if I comment out the category rule...
#RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?content=category&categoryname=$1 [NC,L]
...the url friendly rule for the contact page works (https://localhost/mysite/contact)
So I'm looking for the possibility to exclude some parameter from the category rule to allow for a redirect in some case to another url.
Thanks for any suggestions...
Firstly you need to make sure the contact rule is placed before the more generic one so htaccess can process it first:
RewriteRule ^contact/?$ index.php?content=message [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?content=category&categoryname=$1 [NC,L]
If you have it the other way around the generic rule will always be looked at and match before htaccess even gets to the contact rule.
Note though that the way you wrote your rules it will only match URLs such as
https://exmaple.com/contact
but not
https://exmaple.com/contact/something-else
However, looking at your more generic rule I assume this is intentional.

mod rewrite -new.php to /new

I want to rewrite any page url which contains -new.php to the /new
For example:
example.com/page-new.php
to
example.com/page/new
I am able to remove the ending .php with following rule, but I can't figure out how to match the -new.
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Your rule actually adds .php to the end.
RewriteRule ^(.*)-new\.php$ $1/new [NC,L]
removes -new.php and changes it to /new
EDIT:
After a while of thinking, if you want users to get redirected, use above and add "R=301" to the brackets as third parameter.
OR, if you actually have links in /page/new schema and files in -new.php schema, then you actually wants to remowe /new and add -new.php. If so, then you asked wrong question. Here you go:
EDIT 2:
You also want to redirect /page to page.php. Now you can do this in two ways:
RewriteRule ^(.*)/new$ $1-new.php [NC,L]
RewriteRule ^(.*)$ $1.php [NC,L]
OR
RewriteRule ^(.*)/new$ $1-new [NC]
RewriteRule ^(.*)$ $1.php [NC,L]
First one are two separate rules, first for /new URLs and then for any other.
Second code is like "first change /new to -new, and then apply next rule"
If you want rule to apply only to urls without dots or anything, then replace (.*) with any other rule like for example ([^\.]*)

adding a parameter to a url that has a mod_rewrite value

I have a rewirte rule that rewrites my portfolio urls from
portfolio.php?cat=cat-name&project=project-slug
to
/portfolio/cat/slug/
That all works fine and dandy, I'm wondering if It's possible to add a parameter to the end of the rewritten slug. For example
/portfolio/cat/slug&preview=true
I'm trying to make it so I can preview hidden projects by appending the preview=true to the end. It works if I go to
portfolio.php?cat=cat-name&project=project-name&preview=true
I'm just hoping I don't have to create a rule that has /true I've tied various ways to get the value or preview but thought someone here may have a simple solution
Edit Adding .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^.*$
#portfolio
RewriteRule ^portfolio/?$ portfolio.php [L,NC]
RewriteRule ^portfolio/([a-z0-9_-]+)/([a-z0-9_-]+)/?$ portfolio.php?cat=$1&project=$2 [L,NC]
RewriteRule ^portfolio/([a-z0-9_-]+)/?$ portfolio.php?cat=$1 [L,NC]
RewriteRule ^portfolio/([a-z0-9_-]+)/page=([0-9]+)/? portfolio.php?cat=$1&page=$2&
Add QSA as an option to the back of the RewriteRule (in the [] brackets). E.g.
RewriteRule ^portfolio/([a-z0-9_-]+)/([a-z0-9_-]+)/?$ portfolio.php?cat=$1&project=$2 [L,NC,QSA]
Please look at http://www.workingwith.me.uk/articles/scripting/mod_rewrite for such an example.
You may also include a hidden field with that appropriate value.

Categories