Reputation: 261
I have a website setup where users can claim "/pages", such as
mywebsite.com/example1 or mywebsite.com/example2.
If a user types in a URL that doesn't exist, it obviously redirects to a 404 error page. However, I want it so that if a user types in a non-existent URL containing just letters or numbers (no symbols or such), then it will be redirected to a page I have created stating "url available!".
I configured a this .shtml page so that when a valid URL is entered it shows this:
<h1>available</h1>
<div>mywebsite.com<!--#echo var="REQUEST_URI" --> is available.</div>
However, I do not know how to set up the "validity" part. I want it to only show this page if the inputted URL only contains numbers or letters, no symbols. In other words,
mywebsite.com/@user goes to 404 error page
mywebsite.com/u$er goes to 404 error page
mywebsite.com/user_ goes to 404 error page
mywebsite.com/user redirects to my custom page
mywebsite.com/user123 redirects to my custom page
mywebsite.com/123 redirects to my custom page
I currently have it set up where the 404 error page is the one that states "mywebsite.com/(...) is available" but this causes problems as it will say pretty much anything is available. I have tried blocking certain characters using RewriteRule ^(.*);(.*)$ /error.shtml [L,R=301]
, where each RewriteRule detects if the url contains another symbol and does a 301 redirect to my custom error page. However, this code cannot redirect all symbols, for example if I put RewriteRule ^(.*)$(.*)$ /error.shtml [L,R=301]
to redirect any uses of dollar signs in the url it gives me a server error as "$" is a reserved character. (Unless there is a workaround that I do not know of)! So I am instead going for this method, instead of blocking symbols, just only allowing letters and numbers.
Help is much appreciated! And needed!
Upvotes: 0
Views: 866
Reputation: 1775
Use the following rules in your .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^[a-zA-Z0-9\._]+$ custom.page [L]
If something exist it gets served up normally. If it doesn't exist AND the part of the request uri for the current directory only contains alphanumeric characters it goes to custom.page. If it contains something else, it goes to the default 404 page.
Upvotes: 1
Reputation: 3707
Here's how I would do that:
#If there is no file match
RewriteCond %{REQUEST_FILENAME} !-f
#Or a directory match
RewriteCond %{REQUEST_FILENAME} !-d
#Redirect alphanumeric strings to your custom page
RewriteRule ^[a-zA-Z0-9_.]+$ /my/custom/page [R=301,L]
#If there are symbols, send to 404
RewriteRule .* /error.shtml [R=301,L]
Upvotes: 1