How to redirect a url with a parameter to a custom 404 page? - http-status-code-404

I have hundreds of bad URLs. They all have a parameter in common
i.g parmameter "date"
I've tried this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^ date=(.+?)
RewriteRule ^$ http://domain.com/404 $ [R,L]
The result was that it redirect to a non-existing page and resulted a 404 error
I've also tried
RewriteEngine On
RewriteCond %{QUERY_STRING} ^ date=(.+?)
RewriteRule . - http://domain.com/404 [R=404,L]
The result was a 404 but not my own custom 404 page
How to redirect a url with a parameter to a custom 404 page?

If you have your own custom 404 that always should be displayed when the page can't be found, I would suggest changing that in your virtual host:
ErrorDocument 404 /pathtofile
Or via htaccess.
ErrorDocument 404 /pathtofile
If you would like to redirect based upon date parameter, this should do:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} date
RewriteRule ^(.*)$ 404.html [L]
</IfModule>
Hope this helps, if it does don't forget to check and up vote ;)

Related

AngularJS w/Prerender 404 error on home page

All of my angularjs site works with prerender except for the home page. When crawled, it sends back a 404 page. I have reason to believe it is this line of code in my .htaccess file, RewriteRule ^(.*)$ http://service.prerender.io/https://%{HTTP_HOST}/$1 [P,L] but I am not sure.
<IfModule mod_rewrite.c>
RewriteEngine On
# If requested resource exists as a file or directory
# (REQUEST_FILENAME is only relative in virtualhost context, so not usable)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
# Go to it as is
RewriteRule ^ - [L]
# If non existent
# If path ends with / and is not just a single /, redirect to without the trailing /
RewriteCond %{REQUEST_URI} ^.*/$
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)/$ $1 [R,QSA,L]
# Handle Prerender.io
RequestHeader set X-Prerender-Token "notprovidingthiscode"
RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
# Proxy the request
RewriteRule ^(.*)$ http://service.prerender.io/https://%{HTTP_HOST}/$1 [P,L]
# If non existent
# Accept everything on index.html
RewriteRule ^ /index.html
The issue turned out to be that the .htaccess file was serving example.com/index.html rather than just example.com when accessing the root of the angularjs app. That in turn didn't play well with ui-router because the $stateProvider doesn't serve filenames at the end of urls without being explicit. Accessing example.com/index.html did indeed cause my page to throw a 404 error $urlRouterProvider.otherwise('404');
Adding the following code fixed my issue.
$urlRouterProvider.when('/index.html', '/');
This redirects example.com/index.html to example.com which points to the correct rendering in prerender.io.

.htaccess 500 internal server error when routing api

I'm currently running php on an apache server locally, with a React frontend.
This is how my current .htaccess is laid out:
Options -MultiViews
RewriteEngine On
RewriteRule ^api/(.*)$ api/$1\.php [L]
RewriteCond %{REQUEST_URI} !^/api.*?
RewriteRule ^ index.html [QSA,L]
The bottom condition is so that routing works in my React app. I'm then taking the production build and copying it into my htdocs.
The routing works, however, I want to be able to call the .php files inside my /api directory without using the file extension. So I want anything that comes after /api/ to be redirected to whatever is entered, followed by .php.
E.g. /api/authentication would go to /api/authentication.php, and /api/register would go to /api/register.php, and so on.
With this current setup, I'm getting a 500 internal server error when making requests to /api/authentication etc.
Is there something wrong with my .htaccess file?
Your first rule is looping as you're matching .*. You may use:
Options -MultiViews
RewriteEngine On
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteRule ^api/(.+)$ api/$1.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^api index.html [L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC] will skip rewriting when a URI ends with .php.

Redirect link problems using htaccess

I have made an opencart store on a subdomain, and I was wondering if there is any way to redirect pages cause when I replace the store word to anything it gives me 404 eror page not found.
ex: http://XXX.XXX/store -----------> http://XXX.XXX/mobile (i got the 404 error)
And how I can get rid from the 404 error on opencart 3 store?
You can redirect like this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.net/$1 [L,R=301,NC]
where example.com is the old domain and example.net is the new one
please share your current .htaccess file for more details

my WordPress 404 Not Found error had been gone :)

I used WordPress for the first time and I changed the URL settings and this error appeared
The requested URL /index.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
I tried to find .htaccess file but there's no such file and I tried to create one but the error still appears
I really don't know what to do .. any help, please?
-EDIT: I solved the Error
https://www.youtube.com/watch?v=vgyaK7dpJB0&t=0s&list=LLDSGkl9MD0q2BjLLJ82nAmA&index=5
this youtuber saved my life
You need to add .htaccess file. and add the code below in .htaccess file.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Prerender with Angular UI Router not rendering root page dynamic content

I have prerender working and running for all pages except the root(home) page.
For example:
http://www.example.com/?_escaped_fragment_=
doesn't render the dynamic data. All other pages works like
http://www.example.com/contact?_escaped_fragment_=
It's like angular UI router doesn't know what state the root it's in so it leaves the view blank. However root page http://www.example.com without escaped_fragment does render it correctly.
I have added the fragment meta tag in the header and html5 to true in angular config.
index.html:
<meta name="fragment" content="!">
app.js:
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('!');
.htaccess:
# Change YOUR_TOKEN to your prerender token and uncomment that line if you want to cache urls and view crawl stats
# Change http://example.com (at the end of the last RewriteRule) to your website url
<IfModule mod_headers.c>
#RequestHeader set X-Prerender-Token "MY TOKEN"
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
<IfModule mod_proxy_http.c>
RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
# Only proxy the request to Prerender if it's a request for HTML
RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(.*) http://service.prerender.io/http://www.example.com$2 [P,L]
</IfModule>
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Your .htaccess might have the URL set to /index.php for the homepage. If so, you could change your rewrite rule to this:
RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(index\.php)?(.*) http://service.prerender.io/http://www.example.com$3 [P,L]
I added (index.php)? to capture that out if found and changed the $2 to $3. Don't forget to change the http://www.example.com to your domain.

Resources