.htaccess redirect from non existing file - wordpress

I have a Wordpress installation and what I'm trying to do is the following:
http://domain.com/page.php to redirect to http://domain.com/page/ only if page.php does NOT exists. If it does, it should load page.php
I was thinking about something like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1/ [L]
But sadly, it doesn't work. And also few more similar rules to this doesn't have any effect or return 500 error.

Try this rule as your very first rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.(?:html?|php)$ /$1/ [L,NC,R=301]

Related

301 Redirect to subfolder but with input

I want my wordpress site redirections to remember the url but place it in a subfolder.
So http://www.example.com/test123 has to redirect to http://www.example.com/voetbal/test123
And http://www.example.com/0239 to http://www.example.com/voetbal/0239
My htaccess looks like this.
RewriteEngine On
RewriteBase /voetbal/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /voetbal/index.php [R=301, L]
this makes my site go from http://www.example.com to http://www.example.com/voetbal/
but only the root.
How can i make my htaccess to remember peoples input and place it after the /voetbal/
Try :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /voetbal%{REQUEST_URI} [L,R=301]

WP - Adding Custom Rewrite Rule

I'm trying to add a custom rewrite rule for my WP site so http://example.com/?u=sample-output, is rewritten as http://example.com/sample-output.
The rewrite rule I'm trying to add is:
RewriteEngine On
RewriteRule ^([^/]*)$ /?u=$1 [L]
The existing rewrite rule is:
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
When I add it in, it gives me a 404 error when I visit the page. I'm guessing this is because of a conflict. Any suggestions on how to fix this would be greatly appreciated.
Based on your comments, put the rule above the WP rules and then you can do something like this. And all URLs that will go to the query string would have to use /start/ in it. Then make sure to ignore /start/ in the WP rules like below for all others.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^start/([^/]+)/?$ /?u=$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/start [NC]
RewriteRule . /index.php [L]

Mod Rewrite Create Fake Or Hide Wordpress Directory

I would like to fake or even hide my wordpress directory from
mysite.com/wp-content/themes/twentyfourteen/page.php to mysite.com/directory/page.php
or even mysite.com/wp-content/themes/twentyfourteen/page.php to mysite.com/fake1/fake2/fake3/page.php
Within my .htaccess file im using the following commands:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^wp-content/themes/twentyfourteen/([^/]+)$ /fakedirectory$1 [L]
I;ve also tried
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^wp-content/themes/twentyfourteen/([^/]+)$ /fakedir1/fakedir2/fakedir3$1 [L]
But for some reason this is not working... Any help would be appreciated!! Thanks
You're doing the opposite.
Try this way (assuming your htaccess is in root folder)
RewriteCond %{THE_REQUEST} \s/wp-content/themes/twentyfourteen/([^\s]*) [NC]
RewriteRule . /fakedirectory/%1? [R=301,L]
RewriteRule ^fakedirectory/(.*)$ /wp-content/themes/twentyfourteen/$1 [L]

rewritecond exclude url in .htaccess

I have these working rewrite rules :
# Allow access to assets folder from plugins folders
RewriteRule ^app/plugins/(.+)/assets - [L]
# forbid access to files and folders under app
RewriteRule ^app/.*$ - [L,F]
# rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?action=$1 [L,QSA]
Now I would like to exclude from these rules everything located under http://www.mywebsite.com/wordpress/...
Therefore I've added a new rewrite condition :
...
# rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^\/wordpress\/ [NC]
RewriteRule ^(.*)$ index.php?action=$1 [L,QSA]
But it dosen't work. When I try to access something like this :
http://www.mywebsite.com/wordpress/contact-us/
it redirects me to index.php...
Any help would be really appreciated.
Ok I finally found what was going wrong with all of this :
In fact I did not notice that a .htaccess file was present in my /wordpress directory.
So on the .htaccess of my root directory I've added this rule on top of all the others :
RewriteRule ^wordpress - [L]
Then in the .htaccess of the wordpress directory I've these rules :
RewriteBase /wordpress/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
I don't know if everything is optimized but it works !

RewriteRule wordpress helicon isapi filter 3

I am trying to achieve a very simple yet complicated thing:
Remove index.php from my blog URL
I have permalink setup as:
/%year%/%monthnum%/%postname%/
I have tried atleast 20 examples online in my .htaccess file but none of them work: Either the whole website doesnt work or the entries dont. I get 404s
Few examples I tried:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?p=$1 [NC,L]
and
RewriteRule (.*) index.php/$1 [L]
and
RewriteRule .* /index.php [L]
and
Options +Followsymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
etc etc..
Unfortunately none of them works. Helicon tech support is asking for money to help me so doors are closed there.
Any rewrite rule expert? Will be grateful to you.
Regards,
I am using IIS 6, Win2k3, Wishlist member and the permalink %postname%.
This config file for isapi rewrite by helicon, is the only one that works.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php
Yaroslav you are my hero.
This is a config that works for us
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php
If it does not work for you, there could be hundred of reasons, starting form insufficient permissions and ending with custom IIS configuration, etc.
Try to follow Troubleshooting section of documentation, try to start from enable logging and test some basic rewrites.

Resources