Rewrite url with query parameter - .htaccess

I would like to rewrite a specific url and remove the query parameter. However online I can't seem to find a solution which works for me. I'm pretty new to .htacces files and the working of them.
My file for now, which is in the main folder of my website:
ErrorDocument 404 /404.html
#Remove php extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#Remove html extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
#Remove id query parameter
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^webhook/(.*)$ webhook.php?q=$1
So the normal url is: https://example.com/api/webhook.php?id=10
which I want rewrite as: https://example.com/api/webhook/10
Does anyone know how to create this for this specific case? Thanks in advance!
EDIT:
In my webhook.php file I have this for testing purposes:
<?php echo $_GET['id']; ?>
When I go to the url https://example.com/api/webhook.php?id=10 I get output 10 in the browser.
When I go to the url https://example.com/api/webhook/10
I get this error message:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
And in the serverlogs I found this error:
Request exceeded the limit of 4 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace
Seems there are too many rewrites.
SOLVED
I found that these lines fixed my problem:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/webhook/(.+)$ api/webhook.php?id=$1 [L]
Now when I go to https://examply.com/api/webhook/10 it loads the webhook.php script with query ?id=10. The page now outputs '10'.

RewriteEngine On
RewriteRule ^api/webhook/([0-9]+)/?$ api/webhook.php?id=$1 [NC,L,QSA]
Usage = These rules will make your url https://example.com/api/webhook.php?id=10 to https://example.com/api/webhook/10

Related

.htaccess Rewrite Rules unexpected behavior

I'm running a few .htaccess rewrite rules, to transform the URL path into a index.php query parameter.
So basically when someone visits https://example.com/elections it transforms the URL into https://example.com/?page=elections
Options -Indexes
RewriteEngine on
# Ignore extensions
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|png|gif|svg|pkg)$ [NC]
RewriteRule ^([^/]+/[^/]+/). $1 [R=301,L]
# Rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)$ /index.php?page=$1 [L,QSA]
This works great! There's only one issue, and that is unexpected behavior when there are 3 or more slashes in the URL the visitor entered.
So when someone visits https://example.com/one/two/three it transforms the URL into https://example.com/C:/xampp/htdocs/project/test/test/ (localhost test).
How can I make it so it just returns the regular 404 instead of this unexpected behavior?
(It's odd because it doesn't do that with 2 slashes)
Converting my comment to answer so that solution is easy to find for future visitors.
Problem is due to your first 301 error which causing this unwanted redirect and that too to a wrong target URL.
You can just keep this code in your .htaccess:
Options -Indexes
RewriteEngine on
# Rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?page=$1 [L,QSA]
Make sure to test after clearing browser cache or test in a new browser to avoid old browser cache.

rewriting url with .htacces cause files not to be found

In general, I am trying to understand how .htaccess works. I would highly appreciate it if anyone points me in the right direction. I have been trying to make the following url (with optional parameters) pretty.
mysite/v1.0/foldername
mysite/v1.0/foldername/param1/
mysite/v1.0/foldername/param1/param2/etc
I tried the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ foldername.php [QSA,L]
the problem is that when I get it to pass the parameters it can no longer get the resources. It seems to have changed directory.
.htaccess is in foldername
Also, I would like to know what site i can go to to learn about REQUEST_URI, REQUEST_FILENAME, etc. A site that is not too technical as it's the apache site.
You are incorrectly rewriting the rules correct rule according to your need would be like,
RewriteEngine On
# rule for removing extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)/?$ $1.php [QSA,L]
# below cond means incoming url is nor a file neither a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# actual rules
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?param1=$1 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ $1.php?param1=$2&param2=$3 [L]
Refrences
Reference: mod_rewrite, URL rewriting and "pretty links" explained
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Creating symlink gives 500 Internal Server Error

Applied cmd: ln -s subdir
Edits to .htaccess: added this chunk between RewriteBase / and first RewriteCond line :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{REQUEST_URI} ^/subdir
RewriteRule ^ subdir/index.php [L]
Loading the page (path e.g. www.example.com/subdir) gives 500 Internal Server Error. The log gives: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace. But I don't get it at all, where is it pulling that 10 redirects from?
The website is www.example.com and is running in one db (on Drupal). You can have multisites while running on same root install of CMS and database (but with prefixes). So the new website should open when visiting www.example.com/subdir (its FTP dir is htdocs/sites/example.com.recruiting) More infos: https://drupal.org/documentation/install/multi-site#subdirectory, but like I mentioned before, applying the .htaccess part ends up in internal server error.
Your conditions should mean rewrite if not already /subdir but it is doing reverse.
Try this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(subdir|favicon\.ico)
RewriteRule ^ subdir/index.php [L]

mod-rewrite forwarding without changing URL

I have a small problem with my Apache configuration when creating "pretty" URLs. I've got it to the stage where typing (or linkig for that matter) to
index.html
forwards you to
index.php?pageID=Forside
that is exactly what I want.
But how can I get index.html to stay in the address bar of the browser?
Right now it forwards and changes the URL to the original one.
Here my .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} index\.html
RewriteRule .* http://www.radoor-designs.dk/index.php?pageID=Forside [L]
And before someone comments on it: Options +FollowSymLinks is missing since it triggers an error 500 on a one.com webhotel.
Thanks in advance!
Try the following:
RewriteEngine On
RewriteRule ^index\.html$ /index.php?pageID=Forside [L]
I think this may help you to resolve your problem.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.html$ /index.php?pageID=Forside [L]
This will do the redirect for you whilst showing index.html in the browser window.
Strange that symbolic links creates an error 500,
if you want it to redirect to index.html?pageID=Forside then do
RewriteRule .* /index.html?pageID=Forside [QSA,L,R=301]
I'm not 100% certain what you are trying to achieve with this could you explain a little more?

mod_rewrite and .htaccess - stop rewrite if script/file exists

This is my htaccess:
RewriteEngine On
# Stop if it's a request to an existing file.
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
# Redirect all requests to the index page
RewriteRule ^([^/]) /index.php [L]
Now this forwards everything to my index.php script! It dosen't stop if a script exists. Anyone have any idea why this isn't working? I've looked everywhere, but I don't really understand mod_rewrite (as much as I thought!).
The problem has come about because I've put in <script> tags which point to .js files in my web directory which are then forwarded to the index.php script. The web developer toolbar tells me this. :) And clicking links to the js files in firefox's view source window also shows the index.php output.
thank you.
This is because after processing a rewrite rule the whole process restarts with the new url. The processing of an url can go thru the rules over and over again each time with the changed URL, until there is no more change (no applying rules found).
You need this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php [L]
Don't think of the rules as a program, they are rules which can overlap and must be as specific as possible with each one.
I guess you should add [OR] after the first condition like this:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*) $1 [QSA,L]

Resources