Writing .htaccess Rewrite rules with similar strings - php

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]

Related

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.

navigation issue inside directory with rewrite rule

I have a directory "Users" with 3 files inside it
/index.php
/activity.php
/settings.php
My rewrite Rule says
RewriteRule ^user/([0-9a-zA-Z_]+)/ users/index.php?id=$1 [NC,L]
Now i want to navigate to
http://localhost/user/userid/logs
So i tried
RewriteRule ^user/([0-9a-zA-Z_]+)/logs users/activity.php [NC,L]
But its not working it loads the contents of the index file
I'm going to assume that your second rewrite rule is after the first. The L flag causes rewrite to stop looking at additional rules once it matches. Basically, the first line:
RewriteRule ^user/([0-9a-zA-Z_]+)/ users/index.php?id=$1 [NC,L]
Is matching, and then ignoring the rest. You could try placing the other line above it like so:
RewriteRule ^user/([0-9a-zA-Z_]+)/logs users/activity.php [NC,L]
RewriteRule ^user/([0-9a-zA-Z_]+)/ users/index.php?id=$1 [NC,L]
You could also remove the L flag, but I don't know what the rest of your htaccess is like, so other rules could supercede.
Or you could also try this:
RewriteRule ^user/([0-9a-zA-Z_]+)/?$ users/index.php?id=$1 [NC,L]
RewriteRule ^user/([0-9a-zA-Z_]+)/logs/?$ users/activity.php [NC,L]

.htaccess overwrite another rule to other page

I have the following rewriterules in my .htaccess file:
RewriteRule ^page_edit_(.*) page_edit.php?page=$1
RewriteRule ^page page.php
If I go to /page_edit_12, for example, I see the content of page.php. But I want to see page_edit.php?page=12.
With the following rules, it gives the correct parameter. So if I go with this rules to /page_edit_12, I see the content of page.php?edit=true&page=12
RewriteRule ^page_edit_(.*) page.php?edit=true&page=$1
RewriteRule ^page page.php
Why I can't break the URL so it can reach a another file, also if a match with another URl is found (/page in /page_edit_12)?
Your regex pattern for the second rule matches "/page_edit.php" and rewrites it to /page.php,add a $ at the end of the pattern to restrict it to match /page only.
RewriteRule ^page_edit_(.*) page_edit.php?page=$1 [NC,L]
RewriteRule ^page/?$ page.php [L]
You should add the [L] - Last - Flag to your first line, so it will end the process in .htacces:
RewriteRule ^page_edit_(.*) page_edit.php?page=$1 [L]

OR Condition in htaccess URL re-writing

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

Rewrite Rules in Htaccess

New to htaccess: how can i write rewrite Rules for this,
instead of http://www.x.com/directory.php?state=TX have www.x.com/texas-fishing
instead of http://www.x.com/directory.php?state=WA have www.x.com/washington-fishing
Actually $1 indicates the dynamic part of the Url like(TX,WA..), but here i need complete title of the page. So do i need to modify in script or .htaccess is enough to manage.
If it is enough then how can we manage..
RewriteRule ^texas-fishing$ directory.php?state=TX [NC,L]
RewriteRule ^washington-fishing$ directory.php?state=WA [NC,L]

Categories