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]
Related
I am going in circle on this.
RewriteRule ^Homes-by-price Real-Estate-Listings.php?minPrice=0&maxPrice=999999999 [NC,L]
RewriteRule ^Homes-by-price/([0-9]+) Real-Estate-Listings.php?minPrice=0&maxPrice=$1 [NC,L]
The first line works. When I enter /Homes-by-price and get the page showing
Real-Estate-Listings.php?minPrice=0&maxPrice=999999999
But when I type in
/Homes-by-price/100000
I should get
Real-Estate-Listings.php?minPrice=0&maxPrice=100000
Instead I get a blank web page and the debug console showing me the HTML script for the page.
If I type in
/Real-Estate-Listings.php?minPrice=0&maxPrice=100000
everything displays correctly.
Complete htaccess (The commented out portions where removed for trouble shooting this issue.)
DirectoryIndex default.html
#Block listing of folder contents
IndexIgnore *
RewriteEngine on
#Rewrite rule for force https and www
# located in 000-default.conf
#rewrite rules single listing page (real-estate-listing.php)
#RewriteRule ^home-for-sale/mls/([0-9]+) real-estate-listing.php?mls=$1 [NC,L]
#RewriteRule ^home-for-sale/([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+) real-estate-listing.php?city=$1&address=$2 [NC,L]
#RewriteRule ^commercial-property-for-sale/([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+) real-estate-listing.php?city=$1&address=$2 [NC,L]
#RewriteRule ^land-for-sale/([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+) real-estate-listing.php?city=$1&address=$2 [NC,L]
#RewriteRule ^business-for-sale/([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+) real-estate-listing.php?city=$1&address=$2 [NC,L]
#RewriteRule ^property-for-sale/([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+) real-estate-listing.php?city=$1&address=$2 [NC,L]
#RewriteRule ^investment-property-for-sale/([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+) real-estate-listing.php?city=$1&address=$2 [NC,L]
#rewrite rules for search page showing multiple listings (Real-Estate-Listings.php)
RewriteRule ^Homes-by-price/([0-9]+) Real-Estate-Listings.php?minPrice=0&maxPrice=$1 [NC,L]
RewriteRule ^Homes-by-price Real-Estate-Listings.php?maxPrice=999999999 [NC,L]
<IfModule mod_headers.c>
<FilesMatch ".(js|css|xml|gz|html|php|json)$">
Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>
Both rules should go to same target with first target :
RewriteRule ^Homes-by-price Real-Estate-Listings.php?minPrice=0&maxPrice=999999999 [NC,L]
RewriteRule ^Homes-by-price/([0-9]+) Real-Estate-Listings.php?minPrice=0&maxPrice=$1 [NC,L]
Every request starts with Homes-by-price will be handled by the first rule so the second rule is useless, just put the last rule in the beginning like this :
RewriteRule ^Homes-by-price/([0-9]+) Real-Estate-Listings.php?minPrice=0&maxPrice=$1 [NC,L]
RewriteRule ^Homes-by-price Real-Estate-Listings.php?minPrice=0&maxPrice=999999999 [NC,L]
Test it like this and let me know
Not sure if this is the best solution, but it did resolve the issue.
By use the full URL in the rewrite statement it now works. It now reads:
RewriteRule ^Homes-by-price/([0-9]+)/([0-9]+) https://example.com/Real-Estate-Listings.php?minPrice=$1&maxPrice=$2 [DPI,NC,L]
This is being caused by your first rule. If you remove the first rule, the second rule runs normally. Your second rule is never actually getting run, this is because the first rule still matches the URL. So it makes the change to Real-Estate-Listings.php?minPrice=0&maxPrice=999999999 and then adds /100000 on to the end. Which is causing the error.
Removing the [L] from the first rule should fix this. Since the first rule is still matching the URL it runs and then the [L] flag stops any further rules from being run past it.
The [L] flag causes mod_rewrite to stop processing the rule set. In
most contexts, this means that if the rule matches, no further rules
will be processed. This corresponds to the last command in Perl, or
the break command in C. Use this flag to indicate that the current
rule should be applied immediately without considering further rules.
For more information on how the RewriteRule Flags work, you read the documentation here: https://httpd.apache.org/docs/current/rewrite/flags.html
This will be a niche question but I am having trouble rewriting my url.
I am trying to rewrite from /view.php?user=Alex0111 to view/Alex0111. I also have a second get variable of /view.php?user=Alex0111&id=5 which I want to be view/Alex0111/5
Here are the contents of my .htaccess file
DirectoryIndex Home.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^view/([0-9A-Za-z]+) view.php?user=$1 [NC,L] #doesn't work causes internal error
I've checked this line of code multiple times to the tutorial I am following but I am missing the mark on something.
Replace both of your rewrite rules with:
RewriteRule ^view/(.+)/(.+) view.php?user=$1&id=$2 [NC,END,QSA]
Your first rewrite rule will interfere because it will rewrite the path as Markus mentioned in comments.
The [END] flag will throw an error if you have an old Apache version. In that case, use the [L]
The [QSA] flag tells the server to add any additional query parameters that the user sent. eg: view/Alex01/5?param=value
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 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 ([^\.]*)
I have the following htaccess rewrite rules
RewriteRule ^shows-watch/(.*).html?$ show.php?name=$1
RewriteRule ^shows-watch/(.*)/season-(.*).html?$ show.php?name=$1&season=$2
RewriteRule ^shows-watch/(.*)/season-(.*)/episode-(.*).html?$ show.php?name=$1&season=$2&episode=$3
Now the thing is, the first rewrite rule works just fine
RewriteRule ^shows-watch/(.*).html?$ show.php?name=$1
It's just when I try using the others, only name get variable is being passed and not
$_GET['season']
or
$_GET['episode']
i know it's most likely something simple I'm missing or have done, but I just can't seem to get it working.
Give this a try:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^shows-watch/([^/]+)/season-([^/]+)/episode-([^/]+).html?$ show.php?name=$1&season=$2&episode=$3 [QSA,NC,L]
RewriteRule ^shows-watch/([^/]+)/season-([^/]+).html?$ show.php?name=$1&season=$2 [QSA,NC,L]
RewriteRule ^shows-watch/([^.]+)\.html?$ show.php?name=$1 [QSA,NC,L]
The order is very important so they don't overlap you also need the L flag to stop when needed.
This assumes your .htaccess is on the root folder of your domain along with the show.php file and that you are accessing it like this:
domain.com/shows-watch/Show Name.html
domain.com/shows-watch/Show Name/season-1.html
domain.com/shows-watch/Show Name/season-1/episode-10.html
The first line gets all the links because it matches all the links.
Try reverse the order:
RewriteRule ^shows-watch/(.*)/season-(.*)/episode-(.*).html?$ show.php?name=$1&season=$2&episode=$3
RewriteRule ^shows-watch/(.*)/season-(.*).html?$ show.php?name=$1&season=$2
RewriteRule ^shows-watch/(.*).html?$ show.php?name=$1
Or you can exclude slashes like this:
RewriteRule ^shows-watch/([^/]*).html?$ show.php?name=$1
RewriteRule ^shows-watch/([^/]*)/season-([^/]*).html?$ show.php?name=$1&season=$2
RewriteRule ^shows-watch/([^/]*)/season-([^/]*)/episode-([^/]*).html?$ show.php?name=$1&season=$2&episode=$3