htaccess: redirecting php query string to filename - php

This looks simple but I do not know what thing is going wrong. I am using MAMP on MacOS and mod_rewrite is already enabled. Below is the .htaccess content:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)\.html$ view.php?symbol=$1 [NC,L]
The URL http://localhost/pe/data/view.php?symbol=APPL should be accessed if someone visits: http://localhost/pe/data/APPL.html
I am getting a 404.
The htaccess file exists in /data/ folder.

The 404 results because the rule rewrites the request to /view.php (as indicated by the RewriteBase directive), not /pe/data/view.php as intended.
If the .htaccess file is in the /pe/data directory and the URLs you are requesting are relative to this directory and view.php is located in this directory (as appears to be the case) then you should... remove the RewriteBase directive entirely.
The RewriteBase directive specifies an alternative URL-path to use for relative path substitutions. view.php (without a slash prefix or scheme+hostname) is a relative path. The default path is the directory that contains the .htaccess file (the directory-prefix). So, if you are rewriting to the default path, you don't need the RewriteBase directive at all.
RewriteRule ^([a-zA-Z0-9_-]+)\.html$ view.php?symbol=$1 [NC,L]
Minor point, but the NC flag is entirely superfluous here.

Related

.htaccess link with parameters

I have a page with URL like this:
https://something.com/paste/log.php?log=H7nSEIPaVr
(By the way my homepage is:
https://something.com/paste/index.php
)
I want to make it work by just giving the log parameter like this:
https://something.com/paste/H7nSEIPaVr
And it would redirect me to the original url.
(or it would be better if it just give me the paramter - ?log=xxx)
I have a .htaccess file, but it is not working for me.
(rewrite is enabled, so the htaccess is working well)
RewriteEngine on
RewriteRule /paste/^([a-zA-Z0-9]*$)/? /log.php?log=$1 [QSA]
This probably is the rule you are looking for:
RewriteEngine on
RewriteRule ^/?paste/([a-zA-Z0-9]+)/?$ /paste/log.php?log=$1 [L]
That rule needs to be implemented in the central http server's host configuration. Or, if you do not have access to that, you can instead use a distributed configuration file (typically called ".htaccess") which has to be located inside the http server's DOCUMENT_ROOT folder.
An alternative would be to place that variant in a distributed file inside the /paste folder:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)/?$ log.php?log=$1 [L]

.htaccess reroute to sub-directory

I have site I am running on localhost http://example and I have directory in the root that contains my api so it's http://example/api
In the api folder I have my root directory for the api which is public directory. so /api/public/....
I want to have a .htaccess that redirects the user to the public directory when they hit http://example/api/{api parameters}. So I can have a clean url without having to write this http://example/api/public/{api parameters}
I tried this but to no avail
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/api
RewriteRule ^api/(.*) /api/public/$1 [L]
I got the above from a SO post but still could not get it to work.
Try using:
RewriteEngine On
RewriteBase /api/public/
Quoting a stack overflow answer, "By having the RewriteBase there, you make the relative path come off the RewriteBase parameter."
With this method I believe you will have to rewrite all paths that don't fall under the base directory. If this is a problem, you could create a rule for each page.
Update: You might be able to write a rule that checked for files at that path before/after it checks for the relative path.

Mod Rewrite, ignore directory unless URL has a trailing slash

I am making an url shortener with a base 64 encoding for my urls. problem is I also have a bunch of folders inside my main directory. My directory looks something like this
/
/css
style.css
/handlers
handle_database.php
index.php
.htaccess
And this is the rewrite rule I use to capture the encoded urls
RewriteRule ^([A-Za-z0-9_\-]{3,8})$ index.php?a=$1
And that works. my problem comes when my system generates an url that would be like this http://exampleurlshortener.com/css, in an ideal scenario the rewrite rule would capture the "css" and let my index.php handle the rest, problem is apache adds a trailing slash and it ends up getting inside the css directory.
So what I need is that
http://exampleurlshortener/css -> lets index.php handle the request
http://exampleurlshortener/css/ -> access the actual directory
So far I've had no luck, because apache keeps adding the trailing slash
You need to turn off DirectorySlash to avoid Apache adding a trailing slash. It is also important to turn off Indexes option to avoid Apache presenting directory listing to users.
DirectorySlash off
Options -Indexes
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^([\w-]{3,8})$ index.php?a=$1 [L,QSA]

URL Rewriting .htaccess

I'm having some problems with the .htaccess file. I'm working on a REST API and I'm rewriting my URL's, if I put .htaccess at the root of my server (localhost) everything works fine, but I actually don't want to put my .htaccess file there but in my project directory.
For example I want to type
http://localhost/myproject/player/id/etc..
and not just
http://localhost/player/id/etc...
The problem is that if I use
http://localhost/myproject/player/id/etc..
The redirection works correctly but in my controller.php file the Path variable I retrieve is /myproject/player/id. Is there a way to remove /myproject/ from the URI in the .htaccess file, or more generally is it possible to remove the directory of the folder containing the .htaccess from the REQUEST_URI variable ?
ok. try this- in your .htaccess write-
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^myproject/(.*)$ /$1 [L,NC,R]
it will remove myproject from your url.

Very simple .htaccess mod_rewrite configuration gives 404

Ok, I'm starting to think that the problem is me but... ¿What's wrong here?
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule testpage\.html http://www.google.com [R] #It works
RewriteRule ^mineral/([0-9]+)/?$ ver.php?id=$1 [NC,L] #It doesn't
I have a folder called "WebX" and this .htaccess inside it along with other files from the web.
mod_rewrite is working ok according to phpinfo and everything is running on localhost.
The most courious thing is that if I type localhost/WebX/testpage.html it redirects to Google
but unfortunately for me if I type localhost/WebX/mineral/1 it gives me 404. ¿What happens?
The problem you are having is caused by RewriteBase /. Your .htaccess is in a subdirectory /WebX/, but you are telling mod_rewrite to rewrite rules as if the current directory was /. It tries to load a file ver.php in your www-root, which doesn't exist. If you have verbose error messages with what file it tried to load, you'll notice it says that it tried to load /ver.php and not /WebX/ver.php as you might expect.
The rest you are doing, most notably using a relative url instead of a / prefixed absolute url, seems correct. The correct .htaccess to use would be:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /WebX/
RewriteRule testpage\.html http://www.google.com [R] #It works
RewriteRule ^mineral/([0-9]+)/?$ ver.php?id=$1 [NC,L] #It now should work

Categories