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 ([^\.]*)
Related
I have a couple of rules in my .htaccess file in order to make the URLs a bit cleaner, however, they seem to be cancelling each other out.
The first rule is just to remove the .php from page names,
example : mysite.com/join rather than mysite.com/join.php
RewriteEngine On
Rewrite Condition : %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
The second rule is to make it easier for Users to share their profiles on my site,
example : mysite.com/user1 rather than the actual URL mysite.com/profile.php?user=user1
RewriteRule ^([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]
I've been playing round with them, and they essentially cancel each other out - Any ideas on how I can get them both working?
Thanks
The problem you have is that both conditions are almost identical i.e. anything that ^([_A-Z0-9a-z-+]+)$ matches will also be matched by ^([^.]+)$, so someone accessing mysite.com/user1 will get redirected to mysite.com/user1.php since that is the first rule that is encountered, and it has the L flag to prevent processing more rules. To prevent this happening you need to make the rules different, e.g. perhaps require user pages to be mysite.com/users/user1? Then you could write the rules as
RewriteRule ^([^./]+)$ $1.php [NC,L]
RewriteRule ^users/([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]
Note that you need to add / to the characters not to be matched in the first rule, otherwise it will still match mysite.com/users/user1.
Edit
A couple of other alternatives:
If you were willing to have actions (e.g. join) use URLs such as mysite.com/action/join then you could keep users at the top level e.g.
RewriteRule ^action/([^.]+)$ $1.php [NC,L]
RewriteRule ^([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]
Or if you know the names of all your actions you could put them in an alternation (this would require that you couldn't have a user called e.g. join):
RewriteRule ^(join|login|logout|delete)$ $1.php [NC,L]
RewriteRule ^([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]
I have a website where I want realise such scheme:
If user will type website/user1 I want to redirect user to website/profile.php?user=user1
But I want to redirect website/feed to website/action.php
I changed my .htaccess file to this:
RewriteRule ^([a-zA-Z0-9_\-]+)(/?)$ profile.php?user=$1 [L,QSA]
RewriteRule ^feed(/?)+$ action.php [L,QSA]
When I type website/user1 it works, but not works for website/feed, it seeks for website/profile.php?user=feed
You have to change the order of your rules:
RewriteRule ^feed(/?)+$ action.php [L,QSA]
RewriteRule ^([a-zA-Z0-9_\-]+)(/?)$ profile.php?user=$1 [L,QSA]
It means that if your url matches to the first RewriteRule it will apply it. Otherwise it will go to the next RewriteRule.
From documentation:
The order in which these rules are defined is important - this is the order in which they will be applied at run-time.
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]
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]
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