mod_rewrite changes links in php response - .htaccess

Please can someone explain what is happening here? This .htaccess correctly redirects to that page in the root directory but all the links on the page now include a (non-existent) sub-directory "test" (though the html source doesn't)
RewriteEngine on
RewriteBase /
RewriteRule ^test/([0-9]+)/?$ /view_player.php?player=$1
ps the same thing happened before I put in the "RewriteBase /" and "/" before the view_player.php destination, they were included in my attempt to find out what is happening.
What do I need to do to stop this happening?

In your RewriteRule, you will see there is a section that says ^test/. This is known as a prefix and you can use this to modify your URL and therefore improving the form of the rewritten URL and increasing its relevancy.
So for example, if I had a URL that read:
www.exmaple.com/common-errors/an-error
I might what to include a prefix called errors, and therefore rewriting it to:
www.example.com/errors/common-errors/an-error

Related

Htaccess replace one image with another one

So Ive been making a code which replaces one image with another without changing the link.
So heres the code that I found on one of the forums.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^.*/fredShip1.png$ /fredShip2.png [L]
</IfModule>
So this code not only redirects user to another page but also to a random link.
So original link is http://toss.rf.gd/storage/fredShip1.png though it should have replaced the image with http://toss.rf.gd/storage/fredShip2.png(Just an example) but it sends the user here toss.rf.gd/home/vol8_1/[Account info]/htdocs/storage/FredShip2.png
I added the image too ->
The image
I am really bad at htaccess so make sure correct me if Im wrong. Also english is not my first language so expect some minor mistakes.
EDIT : So i solved the problem with redirection to a random link. But Im still wondering is it possible to just change the image without changing the link?
RewriteRule ^.*/fredShip1.png$ /fredShip2.png [L]
The code you've posted already does essentially what you require, except that you need to adjust the paths to match your example. The "problem" with the above rule is that it rewrites the request to /fredShip2.png (in the document root), not /storage/fredShip2.png as in your example.
Assuming the .htaccess file is in the document root of the site and you wish to internally rewrite the request from /storage/fredShip1.png to /storage/fredShip2.png then you would do it like this:
RewriteRule ^storage/fredShip1.png$ storage/fredShip2.png [L]
There should be no slash prefix on the URL-path in either argument.
If you have other directives in your .htaccess file then the order of these directives can be important.
Make sure you've cleared your browser cache before testing.
but it sends the user here example.com/home/vol8_1/[Account info]/htdocs/storage/FredShip2.png
That's not possible with the directive you've posted. This is most likely a cached redirect due to an earlier (erroneous) experiment with 301 (permanent) redirects. For example, something like the following would produce the above "erroneous" output:
RewriteRule fredShip1\.png$ storage/FredShip2.png [R=302,L]
Note the use of the R (redirect) flag and the lack of a slash prefix on the RewriteRule substitution string (2nd argument). Since the substitution string is "relative", the directory-prefix (ie. /home/vol8_1/[Account info]/htdocs/ in your example) is prepended to substitution and since this is an external redirect (as denoted by the R flag) this then exposes the absolute filesystem path to the user.
NB: The above is a 302 (temporary) redirect - so should not be cached by the browser (at least not by default).

Allow specific URL in htaccess even if htaccess rewrites a word from that URL

I have the following situation in my .htaccess:
RewriteEngine On
RewriteRule ^laravel/(.*)$ /$1 [R=301,L]
This is specifically made to not allow people to visit my laravel directory.
However, I want to be able to load a specific file from laravel directory into other files, like this:
<script src="/laravel/public/js/app.js" defer></script>
The problem is the following:
The generated URL will have 'laravel' removed from it as per the rule. If I comment that rule, then that line of code that includes app.js will work.
I have tried several things with my .htaccess and searched for a solution, but alas, I am failing to understand, it seems, how .htaccess code really does the things.
Can anyone help with a rule to allow specifically that URL?
Or, if possible, to allow access to the /laravel/public/js/ directory without removing the word 'laravel' from the URL.
Thank you very much!
Instead of doing complex things with checking negated patterns in a RewriteCond or similar, you could just put a rule before this that matches that URL specifically, does no rewriting at all (- in place of substitution URL), and then uses the L flag to indicate that none of the following rules should be evaluated any more.
RewriteRule ^laravel/public/js/app\.js$ - [L]

Adding a subdirectory to an invisible .htaccess URL rewrite

I wanted to add a subdirectory to my url so it would become easier to read:
Example of what i'd like:
localhost/testwebsite/users.php?firstname=john
should become
localhost/testwebsite/users/john
I tried using the .htaccess mod_rewrite rules and came up with this:
RewriteEngine On
RewriteBase /testwebsite/
RewriteRule ^users/(.*)$ users.php?firstname=$1
What happens why I use that code: it redirects the page successfully, it shows the html of the correct user and it processes the argument correctly. However, all stylesheets, images, scripts, anything with a relative path, could not be found and respond with a 404 message, because of the extra subdirectory added in the new url, I reckon.
Am I doing something wrong? Is there another technique I should be using? Or should I simply make all paths in my project absolute with regards to the root?
You're doing it right. The browser doesn't know that the actual path of the file is different. Use absolute paths or make paths relative to the easier to read URL.

htaccess file URL rewriting

For my test I have two html pages, TEST.HTML and TEST2.HTML
TEST.HTML has a link to go to TEST2.HTML:
the link is test2.html/city/state/category
This works fine, but when the TEST2.HTML page is loaded it is showing the URL as:
test2.html?var1=city&var2=state&var3=category
I'd like the URL to be clean, and just show:
test2.html/city/state/category, and not the full URL. (does this make sense??!)
Can anyone give me some pointers on where to start? I've tried numerous different things, but it's not working.
Please let me know if you need any further info.
Here's my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^test2.html/([^/.]+)/([^/.]+)/([^/.]+)?$ http:/www.mydomain.com/test2.html?var1=$1&var2=$2&var3=$3
Welcome to Stack Overflow.
By including the domain in your rewrite destination, you are effectively instructing a redirect, and not a rewrite.
If you remove it, it will pass the arguments to the file instead, which is what you want:
RewriteRule ^test2.html/([^/.]+)/([^/.]+)/([^/.]+)?$ test2.html?var1=$1&var2=$2&var3=$3 [L]
I've also included the L flag, which stops rewriting if that particular rewrite has been found.

Simple and neat .htaccess redirect help required

This is a strange one...
A while back I managed to write a .htaccess redirect that worked so that the URL was read like: www.website.com/mt?page=index - and what the real URL of this page was www.website.com/PageParser.php?file=index.php
The problem has been that the FTP system of my webhost hides .htaccess files even though they are allowed and do operate - and so I have checked back on local copies I have of my .htaccess files and none of them have the code as to how this works - and I've forgotten how I did it!!
Essentially, I am using wildcards so that anything after mt?page= will actually be showing PageParser.php?file= but without having the PageParser.php showing within the URL (and this is the important bit, because the index.php on my site root is actually sent through PageParser.php first so that anything which shouldn't be there is wiped out before the end user sees it) - so how can .htaccess redirect/rewrite the URL so that any link to /mt?page= show the file located at /PageParser.php?file= without changing the URL the user sees?
RewriteEngine On
RewriteRule ^(.*)mt?page=(.*)$ $1PageParser.php?file=$2
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^page=([^&]+)
RewriteRule ^mt$ /PageParser.php?file=%1.php [NC,L]
This rule will rewrite (internal redirect) request for /mt?page=hello to /PageParser.php?file=hello.php without changing URL in browser.
Your source URL example (www.website.com/mt?page=index) has index while target URL (www.website.com/PageParser.php?file=index.php) has index.php. The above rule will add .php to the page name value, so if you request /mt?page=hello.php it will be rewritten to /PageParser.php?file=hello.php.php.
If there is a typo in your URL example and page value should be passed as is, then remove .php bit from rewrite rule.
The rule will work fine even if some other parameters are present (e.g. /mt?page=hello&name=Pinky) but those extra parameters will not be passed to rewritten URL. If needed -- add QSA flag to rewrite rule.
This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
P.S.
Better write no explanation (I knew it/I did it before .. but now I forgot how I did it) than having these "excuses". While it may be 100% true, it just does not sound that great.

Resources