Reputation: 966
I want to have whenever the user types a "Not Found" link (using WAMP Server) on my server to redirect to a certain directory with the subdirectory as what the user originally typed.
What I mean is, if the user wants to access smiskol.com/hello
, and it doesn't exist, then the page gets redirected to smiskol.com/specific-page/hello
where the page the user tried to access is appended to the end.
I cannot for the life of me figure it out, as I'm new to the syntax of .htaccess. So far I have the following, but it doesn't work:
Options -Indexes
Redirect 302 /drop /file-drop
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /short-links/%1 [L]
Thanks!
Upvotes: 1
Views: 320
Reputation: 966
Thanks to @Eduardo Escobar and some extra time, I finally came up with a good solution!
This is the final code I used:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/short-links/$1.php -f
RewriteRule ^(.*)$ /short-links/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/short-links/$1.php !-f
RewriteRule ^(.*)$ /index.php [L]
The first chunk redirects to /short-links/hello
if hello.php
exists in the short-links
directory and if the page doesn't exist by itself, while the second redirects the user back to the index page if the page doesn't exist and the short-link doesn't exist. Viola!
Upvotes: 1
Reputation: 3389
In your last line, use this instead:
RewriteRule ^/?(.*)$ /short-links/$1 [L]
Upvotes: 1