.htaccess overwrite another rule to other page - php

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]

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]

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 with multiple parameters not working,?

I am trying to use htaccess file to rewrite urls, I really have zero experience at this and everything I have done is from searching here and on google. My links have query parameters in them. I send everything to an index.php page.
I have tested all the links before trying to rewrite them and they all work in the index page. So then I created an htaccess file to rewrite the links but all I get is a blank page, none of the variables are being sent to the index page. I use $_GET in the index.php page to retrieve the variables.
Here is a list of type of links I am trying to rewrite along with a copy of my actual htaccess file, also mod rewrte is on and functioning properly in apache. Please help me change my htaccess file to do EXACTLY what I am wanting according to the sample links I have shown below, please?
The directory rewrite is fine and works accordingly, it is the links that don't.
Also, if you notice in links #1 and #2 the page parameter does not and should not appear in the rewritten links for these 2 links ONLY but a blank or empty string is still sent to my index.php.
example--
<?php
$page= $_GET['page'];
$user= $_GET['user'];
$act= $_GET['act'];
$section= $_GET['section'];
$search= $_GET['search']
$xx= $_GET['xx'];
if($page == ''){
//Do some code
}elseif($page == 'search'){
//Do some code
?>
Rewrite these type of links
note - "http://" is front of all of these links, stack overflow wouldn't let me add it and post.
LINK #1
localhost/notetag2/johnsmith/all/1
--REWRITE TO--
localhost/notetag2/index.php?page=&user=johnsmith&act=all&section=1
LINK #2
localhost/notetag2/johnsmith/all
--REWRITE TO--
localhost/notetag2/index.php?page=&user=johnsmith&act=all
LINK #3
localhost/notetag2/search/johnsmith
--REWRITE TO--
localhost/notetag2/index.php?page=seach&search=johnsmith
LINK #4
localhost/notetag2/pictures/1
--REWRITE TO--
localhost/notetag2/index.php?page=pictures&xx=1
Rewrite directory
pages
--REWRITE TO--
web
HERE IS MY ACTUAL HTACCESS FILE
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /notetag2/
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=$1&user=$2&act=$3&section=$4 [L,NC]
RewriteRule ^(.*)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=$1&user=$2&act=$3 [L,NC]
RewriteRule ^(.*)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=$1&user=$2 [L,NC]
RewriteRule ^(.*)/$ notetag2/index.php?page=$1 [L,NC]
RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=search&q=$1 [L,NC]
RewriteRule ^pictures/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=picturesh&xx=$1 [L,NC]
RewriteRule ^([a-zA-Z0-9_-]+)/?$ $1.php [L,NC]
RewriteRule ^pages/(.*) web/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /notetag2/index.php [L,QSA]
</IfModule>
You need to fix a couple of your regular expressions. Since your htaccess file is in your notetag2 directory, you don't need to match against the leading "notetag2".
But first, your rules are not in the right order. If you want to match /search/something, then you can't have a rule that matches anything, otherwise it'll match /search. So these rules need to be at the top:
RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ index.php?page=search&q=$1 [L,NC]
RewriteRule ^pictures/([a-zA-Z0-9_-]+)/?$ index.php?page=picturesh&xx=$1 [L,NC]
You also don't need the notetag2/ in your rule's target since that's implied because the rules are in the notetag2 directory.
So after those 2 rules, you need to take each of your other rewrites starting from the largest to smallest:
localhost/notetag2/johnsmith/all/1
--REWRITE TO--
localhost/notetag2/index.php?page=tutorial&user=johnsmith&act=all&section=1
You almost had that, just had some random (.*)/ in front of the regex, which will make it never match what you want. You want this instead:
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=tutorial&user=$1&act=$2&section=$3 [L,NC]
Then for the next one:
localhost/notetag2/johnsmith/all
--REWRITE TO--
localhost/notetag2/index.php?page=tutorial&user=johnsmith&act=all
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=tutorial&user=$1&act=$2 [L,NC]
The "search" and "pictures" should have been taken care of by the first two rules above.
Whatever other rules you had at the bottom of your old htaccess file, they need to go below these rules.
So that leaves only:
pages
--REWRITE TO--
web
Since these don't start with notetag2, you need to put these rules in a different htaccess file, the one in your document root:
RewriteRule ^pages/(.*)$ /web/$1 [L]

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 ([^\.]*)

rewrite a folder so all files display under new rewrite rule

Is there a way that I can rewrite a folder so that all the files under that folder follow the same rule? For example:
if i have a folder with say 5 php files (a.php, b.php, c.php, d.php, index.php) in it and i use the following rule:
RewriteRule ^products/storage/?$ /content/products/storage/index.php [QSA,L]
is there a way that I can get all the files to show to be accessed like: site.com/products/apples/a.php, site.com/products/apples/b.php, etc. without having to write a rule for each one?
I tried the following but it didnt work.
RewriteRule ^products/storage/?$ /content/products/storage/ [QSA,L]
I also need it to NOT overwrite my other rules such as:
RewriteRule ^products/storage/?$ /content/products/storage/product-name1/ [QSA,L]
RewriteRule ^products/storage/?$ /content/products/storage/product-name2/ [QSA,L]
any ideas?
Your problem is the trailing $ on the end of the regex. This will only allow a match if the full URI matches products/storage (with optional trailing slash) exactly. Instead, try the following and note the absence of the trailing $ character:
RewriteRule ^products/storage/? /content/products/storage/ [QSA,L]
This will match anything that starts with products/storage (with optional trailing slash). Alternatively, if you wanted to capture and re-use everything in the URI that followed products/storage/ you could try:
RewriteRule ^products/storage(/?.+)?$ /content/products/storage$1 [QSA,L]
UPDATE
Should you need to preserve other RewriteRules as your updated question suggests, you should look to add a RewriteCond condition like so:
RewriteCond !^products/storage/?$
RewriteRule ^products/storage(/?.+)?$ /content/products/storage$1 [QSA,L]
The RewriteCond tells the RewriteRule to only process if the condition is not met.

Categories