.htaccess file (save request_filename) as querystring - .htaccess

I am using my htaccess file to forward users to a specific page when they request a notfound page. So far i have this:
ErrorDocument 404 /notfound.asp
I also want to pass the bad filename that they tried to use to this page using a querystring value. So I want something like
ErrorDocument 404 /notfound.asp?badfilename=HERE
I have been trying to play wround with URL rewrite and using %{Request_Filename} but I am new to working with .htaccess code. Is it possible to pass the bad filename to the notfound.asp with the bad file name as a querystring value? (I have code in my notfound.asp page that will retrieve the bad file name querystring value for tracking purposes.)

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /notfound.asp?badfilename=$1 [L,QSA]
Reference: Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details

Related

.htaccess rewrite request from OLD rest endpoint NEW endpoint

I am trying to include in my .htaccess file a condition to rewrite requests to a REST endpoint that will no longer exist after I perform some updates to the location of the new endpoint. The existing request looks something like this:
https://www.example.com/applications/interface/?info&key=xxx&indentifier=xxx
I would like the new request to be similar structure, just in a different location, in the community folder:
https://www.example.com/community/applications/interface/?info&key=xxx&indentifier=xxx
Any help would be greatly appreciated. My existing .htaccess looks like below:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpeg|jpg|gif|png|ico|map)(\?|$) /404error.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Is there a reason why you can't just redirect the old request to the new one? Does the URI need to be internally rewritten?
If a redirect is good enough, you can do something like this (note that I'm using mod_rewrite and not mod_alias' Redirect directive, this is because they both end up getting applied to the same request and may conflict with your existing routing rule)
RewriteRule ^applications/interface/$ /community/applications/interface/ [L,R]
And you'd want that right under RewriteBase. This will take requests that look like /applications/interface/ and redirect them to /community/applications/interface/ with all of the query string parameters intact. The regex will only match that URI specifically, with the trailing / and as well.
If you need to do some sort of internal rewrite, I'd suggest doing this within your framework instead of trying to use mod_rewrite in the htaccess file. You've got it setup so every request is routed to index.php and it's up to that code to figure out what needs to go where.

Dynamic 404 Page.. kinda

Alright, so this is what I am trying to achieve.
I want to be able to send URL data to a php file if the server returns 404.
for example:
example.com/stackoverflow
would bring up a dynamic 404 page
(404.php?id=stackoverflow)
but I am not using it for 404 in my case, I want to send the data after the domain.com/
So that I can pull from my database and display content accordingly.
I know this can be done with a small rewrite in the .htaccess, regex is just confusing for me.
I do NOT want a redirect.
http://example.com/datahere
should show the data of
404.php?id=datahere
You can use ErrorDocument directive to redirect 404 uris to /404.php something like the following :
ErrorDocument 404 /404.php?id=%{REQUEST_URI}
This will rewrite /datahere to /404.php?id=datahere ( /datahere will show you the contents of /404.php?id ) .
Note that the above directive doesn't work on apache versions bellow 2.4 as the mod-rewrite variable %{REQUEST_URI} is treated as plain text not as a variable on lower versions.
On apache 2.2 you could use mod-rewrite to rewrite non-existent requests to 404 . Use the following :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /404.php?id=$1 [L]

How to rewrite url for certain pages in htaccess?

I am trying to show different url and redirect users to specific url with .htcaccess when they click on a blog post but to no avail.
Lets say the url is: http://localhost/mySite/article.php?article_title=test-title
then I would like to show it as http://localhost/mySite/article/test-title
This is my current htcaccess file:
#turn on url rewriting
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#rewrite rule for blog
RewriteRule article/([A-Za-z0-9-]+) /mySite/article.php?article_title=$1
But for some reason it is not redirecting/showing the correct url. I am not getting any errors.
EDIT
Trying to ask my question again and explain it better. Let's say the url is
http://localhost/www.example.com/admin/editUser.php?user_id=126
and I would like to rewrite the url like this:
http://localhost/www.example.com/admin/user/126
then how can I achieve this. I tried using this website to check the modified url but it does not work. Seems like it does not work with any of the accepted answers here in stack at all.
This is my htaccess file atm. It is in the root of www.example.com
#turn on url rewriting
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^user/([0-9]+)/?$ /editUser.php?user_id=$1 [NC,L] # Handle user edit requests
Apache Module mod_rewrite is enabled. Also added an alias. Still no changes in the url. If I try something really basic like this:
# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]
it works fine.
Why is the users redirect not working? What am I doing wrong.
Try it like this for your rule for article url in mysite directory.
RewriteRule ^article/([A-Za-z0-9-]+)$ article.php?article_title=$1 [QSA,NC,L]
you need to mention start ^ and end $ of string.

rewrite rule with .htaccess to mask url

My present url structure :
domain.com/items/view/5
domain.com/user/view/5
domain.com/user/edit/5
Now i don't want users to directly know the 'id' in the url, as they can directly fire a query from the address bar.
Hence i want to mask the url to :
domain.com
i.e. domain.com/anything will come as it is but the url will not change.
Thanks in advance.
Also note that i have already made .htaccess file with following code to remove 'index.php' from the url and that is working perfect.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Can't be done. The only way to hide the id is to pass it outside the url - i.e. as POST data; but .htaccess doesn't have access to that, only the url.
Your code should instead handle the fact that users could enter the url directly and either act correctly or use a http-redirect header to send the user back to the main page.

404 redirect using .htaccess

I want to set up a rule in my .htaccess file so that any url that is enetered, that results in a 404 because there is no such file, automatically re-directs to the home page of the site:
index.php
my .htaccess file looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^queenslandbeerweek.com.au$ [NC]
RewriteRule ^(.*)$ http://queenslandbeerweek.com.au/$1 [L,R=301]
ErrorDocument 404 /index.php
This causes the index.php file to show but is broken and leaves the eroneous URL in the address bar.
I have read in the answer to another post that it has something to do with passing the erroneous URL as a parameter, causing the page to not load properly, because the page calls data from a database and it is passing the bad URL as a parameter of index.php but there was no hint as to what the solution is.
What I would like to happen, is if an incorrect URL is typed into the address bar, or if a link is followed, to a file that does not exist, the completely forget about this file, drop everything, and go to the home page index.php.
index.php calls data from a database
Is this possible using a .htaccess file?
I have exactly the same problem with another of my sites.
Any help would be greatly appreciated.
Cheers, Al.
I dont think you can directly redirect an error document but you can catch nonexistent files and folders
!-f means not a file !-d means not a directory, $1 is whatever is in (.*) (the path in the url)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?errorurl=$1 [R=301,L]
ErrorDocument 404 /404.php
You can place the 404 error template anywhere you want. For example you could place all error messages in a folder called errormessages
ErrorDocument 404 /errormessages/404.php

Resources