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
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]
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 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]
So, I've a big problem with .htaccess. All addresses are redirected to app/webroot. I'd like to make an exception without redirection for my file: cron-5min.php.
My part .htaccess:
RewriteRule ^cron-5min.php$ cron-5min.php
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
Unfortunately, when I type address.com/cron-5min.php it redirects me to file for path /app/webroot/cron-5min.php
I tried to add instruction RewriteRule ^cron-5min.php$ cron-5min.php in a variety of places in .htaccess, but nothing helped.
Thanks in advance!
Your rewrite rule does not terminate the chain. Note your other two rewrite rules are appended with [L] (Last). This indicates that no more rules should be processed when it matches.
You can also use the [END] flag, which is the same as [L], but no more rules will be processed from subsequent .htaccess files in this requrest.
More info on both these flags and using flags are in the documentation:
http://httpd.apache.org/docs/current/rewrite/flags.html
I'm working on a website that has been built sloppily.
The website is filled with regular links that are translated into the corresponding .php pages by the .htaccess page.
This is it:
RewriteEngine on
RewriteRule ^koral/(.*)/$ page.php?name=$1
RewriteRule ^koral/(.*)$ page.php?name=$1
RewriteRule ^(.*).html/(.*)/(.*)/(.*)$ cat.php?cat=$1&page=$2&order=$3&dir=$4
RewriteRule ^(.*).html$ cat.php?cat=$1
RewriteRule ^(.*)/(.*).html$ product.php?cat=$1&product=$2
<IfModule mod_security.c>
SecFilterEngine Off
</IfModule>
First of all, I would love some help regarding whether or not this page has everything it should. I've never messed with it before.
Secondly and my main issue, if, for example, I would write the address www.thewebsite.com/foobar.html, it would be translated into www.thewebsite.com/cat.php?cat=foobar by the .htaccess page, and it would give a database error (and reveal information about the database).
I've put a check into cat.php which checks if the category exists, but I can't redirect the user to the 404 error page. There's a page called 404.shtml in the website, but redirecting the user to it causes the .htaccess to just change it again to cat.php?cat=404.
Is the way they used the .htaccess page normal? Should I change this system?
And how are users sent to error pages? From what I understood the server should be doing it on its own?
I would love some clarification... There is some much about this subject I don't understand.
Update:
This is my new .htaccess page
RewriteEngine on
RewriteRule ^error.php?err=(.*)$ Error$1.html
# Only apply this rule if we're not requesting a file...
RewriteCond %{REQUEST_FILENAME} !-f [NC]
# ...and if we're not requesting a directory.
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^koral/(.*)/$ page.php?name=$1
RewriteRule ^koral/(.*)$ page.php?name=$1
RewriteRule ^(.*).html/(.*)/(.*)/(.*)$ cat.php?cat=$1&page=$2&order=$3&dir=$4
RewriteRule ^(.*).html$ cat.php?cat=$1
RewriteRule ^(.*)/(.*).html$ product.php?cat=$1&product=$2
<IfModule mod_security.c>
SecFilterEngine Off
</IfModule>
Because the redirecting is in the code and the user cannot see it, I allowed myself to write the link in a non-clean way. I tried turning it into a clean URL but the following does not do anything:
RewriteRule ^error.php?err=(.*)$ Error$1.html
Can someone please help me understand why? I thought since error.php is a real page, I should put it before the conditional but it didn't work. BTW, I saw in an article about .htaccess that the page should start with Options +FollowSymLinks. It seems to me that everyone sort of has their own way of writing it. Is there a guide or something like that, which I can be sure is authentic and covers all the bases there is about .htaccess?
Thank you so much!!
Using rewrite rules to work around links to .html pages that don't exist is unusual in my experience, but it's really just a different take on "pretty" URLs, e.g. www.thewebsite.com/foobar/ gets routed to cat.php?cat=foobar on the backend.
Your 404 issue is different. You need to be able to display error pages.
One option here is to rewrite requests as long as they don't request an existing file. This is very common for serving up static content like images, CSS files, and the like. To do this, you can use the -d and -f options to RewriteCond, which apply when requesting a directory and file respectively:
RewriteEngine On
# Only apply this rule if we're not requesting a file...
RewriteCond %{REQUEST_FILENAME} !-f [NC]
# ...and if we're not requesting a directory.
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^([^.]+)\.html$ cat.php?cat=$1 [L,QSA]
Now, requests to 404.shtml should go through, because you're requesting an existing file on the filesystem.
Note that the RewriteConds only apply to the single RewriteRule that immediately follows. For additional RewriteRules, also include additional RewriteConds.
Your regex is wrong anywhere. Literal dot needs to be escaped using otherwise it will match any character. Also it is better to use L and QSA flags to end each rule properly.
RewriteEngine on
RewriteBase /
RewriteRule ^koral/([^/]+)/?$ page.php?name=$1 [L,QSA]
RewriteRule ^([^.]+)\.html/([^/]+)/([^/]+)/([^/]*)/?$ cat.php?cat=$1&page=$2&order=$3&dir=$4 [L,QSA]
RewriteRule ^([^.]+)\.html$ cat.php?cat=$1 [L,QSA]
RewriteRule ^([^/]+)/([^.]+)\.html$ product.php?cat=$1&product=$2 [L,QSA]