htaccess - redirect 404 error - .htaccess

I would like to redirect in htaccess
https://www.example.com/albumgallery.php?id=8
to https://www.example.com/example-gallery
i've tried :
RewriteCond %{QUERY_STRING} ^id=8$
RewriteRule ^albumgallery.php$ https://www.example.com/example-gallery? [L,R=301]
-- it works , but on page it says
Not Found The requested URL /example-gallery was not found on this
server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
any ideas what is wrong with that ?

You are getting a 404 not found error because www.example.com/example-gallery does not exist on your server. You need to rewrite this url to your existing url. for example when you type
https://www.example.com/albumgallery.php?id=8 into your browser your rule redirects it to https://www.example.com/example-gallery since this new url doesn't exist so your server returns a 404 error status. You need to add an internal RewriteRule to your htaccess to handle your new url.
Put the following right bellow your existing Rule
RewriteRule ^example-gallery/?$ /albumgallery.php?id=8&loop=no [L]
I added an additional perameter loop=no to the Rule`s destination it's to prevent infinite loop error so that the rule can not conflict with your existing RewriteRule.

Related

throw 404 error with .htaccess file and php

I've got url like www.web.com/home and www.web.com/about with .htaccess file:
RewriteEngine On
Options +FollowSymLinks
ErrorDocument 404 http://www.web.com/page-not-found.html
RewriteRule "home" "index.php?c=home&m=main"
RewriteRule "about" "index.php?c=home&m=about"
If I type something like www.web.com/asd, .htaccess will throw 404 error and direct page-not-found.html file
But, If I type www.web.com/homesss or www.web.com/about/a/b/c, the .htaccess will not throw the 404 error
How do I fix that? I use pure PHP
Use meta characters in rewrite rule to 1:1 match:
^ --- Matches the beginning of the input
$ --- Matches the end of the input
[L] --- Flag last to stop processing rule after match
RewriteEngine On
Options +FollowSymLinks
ErrorDocument 404 http://www.web.com/page-not-found.html
RewriteRule ^home$ index.php?c=home&m=main [L]
RewriteRule ^about$ index.php?c=home&m=about [L]
In addition to #JarekBaran's answer...
If I type something like www.web.com/asd, .htaccess will throw 404 error
Not with the code you've posted...
ErrorDocument 404 http://www.web.com/page-not-found.html
When a request does not map to a resource, the above triggers a 302 (temporary) redirect to the http://www.web.com/page-not-found.html. There is no 404 HTTP response. Details of the request that triggered response are lost. /page-not-found.html is exposed to the end user. This is generally bad for SEO and delivers a bad user experience.
The 2nd argument to the ErrorDocument directive should be a root-relative URL-path, starting with a slash so that the error document is called with an internal subrequest. Apache then responds with a 404 status. The error document is not exposed to the end user.
(Very rarely should an absolute URL be used here.)
For example, it should be like this instead:
ErrorDocument 404 /page-not-found.html
Reference:
https://httpd.apache.org/docs/2.4/mod/core.html#errordocument

URL rewrite for / causing 500 internal server error

I have a rewite rule that should redirect the index page (/) to a different page so I can deal with all of my content in there. However adding this second rule caused a 500 internal server error and I am not sure why.
This is in my .htaccess file
Its the 3rd line:
RewriteEngine on
RewriteRule ^search/(.*)/?$ search.php?postcode=$1
RewriteRule ^(.*)$ /turnstile.php?goto=home [L]
You have to exclude the destination you are rewriting to.
RewriteRule ^((?!turnstile.php).*)$ /turnstile.php?goto=home [L]

.htaccess RewriteRule Redirect works on htaccess tester, does not work on server

I have an .htaccess file defined at www.mydomain/documentation/.htaccess and am trying to redirect a number of broken links to a specific page.
The .htaccess file contains the Rewrite Rule
RewriteRule ^/?(documentation/PresentationCore~System\.Windows\.RoutedEventArgs).*$
/documentation/v4.x/SciChart_WPF_v4_SDK_User_Manual.html [R,L,NC]
Note: RewriteEngine ON has already been defined. Other rules are being applied
Now in .htaccess tester the rule is being evaluated and redirects correctly when the following URL is requested:
www.mydomain.com/documentation/PresentationCore~System.Windows.RoutedEventArgs~Handled.html
However, when the same domain is entered into the browser, I get a 404 not found for this page.
I can confirm I have uploaded .htaccess to the correct folder and cleared the server cache.
Is there anything else I need to do to get this to work?
You dont need to match the folder name if htaccess is already in that folder, try :
RewriteRule ^/?(PresentationCore~System\.Windows\.RoutedEventArgs).*$
/documentation/v4.x/SciChart_WPF_v4_SDK_User_Manual.html [R,L,NC]

htaccess rule is true but site gives 404

I'm struggling to see why this rule won't work on the site.
The old url was /mobile/article/site/article-name and the new url is just /article/article-name
The rule is
RewriteRule ^mobile/article/tgc/(.*)$ /article/$1 [L,R=301]
Testing against http://htaccess.madewithlove.be/ with the whole htaccess file tells us
This rule was met, the new url is http://site.dev/article/article-name
Test are stopped, because of the R in your RewriteRule options. A redirect will be made with status code 301
but when we try hit http://site.dev/mobile/article/site/article-name in the browser, we get a 404 rather than a redirect.
What's even more curious is that we have the same kind of rule for another url
RewriteRule ^resources/a/(.*)$ http://resources.site.dev/library/$1 [L,R=301]
Which comes after our troubled redirect and it works just peachy.
What are we missing?
Keep your rule as this:
RewriteRule ^mobile/article/site/(.*)$ /article/$1 [L,NC,R=301]

404 not FOUND when there is a dot(.) in the url (lighttpd)

I have a CodeIgniter app and have an .htaccess file with this code
RewriteEngine on
RewriteCond $1 !^(index\.php|file|test.php|lib|resources|xmlhttp|dashboard.html)
RewriteRule ^(.*)$ /index.php/$1 [L]
All urls are working fine except for this url
http://myweb.com/login?site=http://google.com
When I type that url in the address bar of a browser and press enter, I will get 404 NOT FOUND from lighttpd, and not from CodeIgniter (custom 404 error page)
The cause of the 404 NOT Found is the dot(.) from google.com bcause when i remove it, it is working as what i want
But when I use the url
http://myweb.com/index.php/login?site=http://google.com
It workds as i expected
My question is, is this because of the htaccess file? or apache configuration? or something else?
Any help or ideas will be greatly appreciated
Thanks

Resources