Can't rewrite php url using htaccess - php

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

Related

htaccess mod_rewrite rule for one parameter

how can I write something like this:
RewriteCond %{QUERY_STRING} s=(\w+)
RewriteRule ^ wordpress/?s=%1 [L]
I need do rewrite rule from domain.com?s=something to domain.com/wordpress/?s=something.
Folder with wordpress is by symlink but It is not interesting.
Rule what I was send works but makes error 500 on other URLs like domain.com/something?company=1
Thanks
Btw: I must send this "body" twice for successful validation. Wtf #stackoverflow? And why are you cut greeting?
You just need this single rule:
RewriteEngine On
RewriteRule ^/?$ wordpress/ [L]
Original query string is automatically copied over.

.htaccess - How to make an exception without redirection

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

htaccess Redirect Causes Errors

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]

htaccess - take all characters after as get request

The real content is at domain.com/view.php?id=image_id
I want to have it be accessible with domain.com/view/image_id
The following is currently there to get the .php file extensions out.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Appending the following to the htaccess doesn't work:
RewriteRule ^view/([A-Za-z0-9]+). /view.php?id=$1 [L, QSA]
Also, I am fine with having everything after view.php? (view.php?id=) get taken into the php file and from there I can separate everything out by slashes and implement their data.
I have looked at many solutions here on stackoverflow and elsewhere but either they do not apply to me or something is wrong with my server. Either way, I get an internal 500 server error. Thanks.
You need to place the view rule before the more general php extension rule:
RewriteEngine On
RewriteRule ^view/([A-Za-z0-9]+)$ /view.php?id=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Also, you don't want that space after the L,.
^view/([A-Za-z0-9]+).
^---single character wildcard
That . at the end there is matching/consuming one of your ID values, so if the url is
/view/foobarbaz
Your capture group will actually be foobarba, and the z gets chopped off, producing
/view.php?id=foobarba
Your 500 Internal Server Error is the result of a redirect loop.
This is because your rule will always be true, because you are redirecting back to the same page.
You need to exclude the resultant link by adding a rule with ^! before all the rest.

Rewritten URL shows 404 errors

I have re-written my URL from website.com?id=1 to website.com/1 and I'm getting 404 errors when trying to access the page and cannot think of a solution to this. I'm currently developing a link shortener. This is required so users will be able to access their shorted links.
This is my current .htaccessfile
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /(index\.php)?\?id=([0-9]+)([^\ ]*)
RewriteRule ^ /%3?%4 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ /?id=$1 [L,QSA]
I cannot figure out whether this has something to do with the .htaccess file or if I need to add something else to my php code.
Would someone have some sort of idea? Thanks.
You need to explicitly rewrite back to index.php in your second rule. By the time rewrite rules are processed the DirectoryIndex directive has already been processed (or may never be processed at all - it depends a little on your virtual host configuration and in what scope the DirectoryIndex directive was declared).
The end result of this is that you need to explicitly rewrite the request to the script that you want to handle the request, you can't just rewrite it to the root of a directory. Try changing your second rewrite rule to:
RewriteRule ^([0-9]+)/?$ /index.php?id=$1 [L,QSA]
On a personal note, it's interesting to see someone else use the %{THE_REQUEST} approach to this problem, this is an idea that I myself only recently came up with, although presumably I am not the first to do so. For the benefit of future visitors, here is a related post that explains why this requirement would come about and the thinking behind it.
I think you have written wrong rewrite rules.
They must be something like this:
for example.com/website.php?id=x..
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([^/]+)$
RewriteRule ^website\.php$ %1/ [L]
as discussed here: https://stackoverflow.com/a/4951918/2274209
Hope this will solve your query.

Categories