Redirection Subfolder Htaccess - .htaccess

I want a redirect using htaccess I have a post name wordpressurl/curious and also a folder named curious
What I want is if a user tries to access
wordpressurl/curious
wordpressurl/curious/
wordpressurl/curious/subfolder
note trailing slash in 1 and 2 and 3 is subdirectory. All of them should give 404 error wordpress page using htaccess only
Edited
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Wordpress_Work/realestate/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /Wordpress_Work/realestate/index.php [L]
</IfModule>
# END WordPress

You can access the wordpress post that is the same as a curious directory by making the changes indicated below. Change curious to whatever your actual directory/post is.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Wordpress_Work/realestate/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
#add an OR
RewriteCond %{REQUEST_FILENAME} !-d [OR]
#OR if the URI is curious or curious/ or curious/subfolder
RewriteCond %{REQUEST_URI} ^/Wordpress_Work/realestate/curious(/.*)?$
RewriteRule . /Wordpress_Work/realestate/index.php [L]
</IfModule>

If you are putting RewriteCond %{REQUEST_FILENAME} !-d you can't have same name for the page and physical directory . that's the point of using it.
Try skipping the rewritecond , though I've never tried it before . for me , I'd change the page name or add a suffix.

Related

Remove Trailing Slash From URLs

I'm trying to remove the trailing slash from our URLs. I adjusted our .htaccess file and added this code:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
Unfortunately this didn't work as the pages (except for the homepage) suddenly show too many redirects and I get a message from the system saying "The page isn’t redirecting properly".
I was thinking maybe I made an error in the way I inserted the code. Originally, our .htaccess file has this:
# 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
# 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
After adjusting it to insert the code, it looked like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# BEGIN WordPress
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And that resulted in an error. Can anybody point out where I went wrong? Thanks.
Turns out it's a Wordpress issue. It's something you can adjust by going to your Wordpress site's Permalink Settings. From here you can choose your preferred URL format. "With-trailing-slash" is the default format so you definitely need to make an adjustment if you want the typical "without-trailing-slash" format.

.htaccess - This webpage has a redirect loop

I'm cleaning my URLs and everything looks fine but whenever I try to access any directory such as images etc then chrome shows that "This webpage has a redirect loop." However I want to protect directories inside public_html.
The .htaccess file is inside public_html
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
</ifModule>
I want to protect images, css and javascript directories but want to allow access to the admin directory.
Thank in advance.
Change order of your rules and change your trailing slash removing rule:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]
# block all directories except admin/
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !^admin(/|$) - [NC,F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</ifModule>
You can use this remove all code and use this one
option index allow and disallow to be index directory like
This does the trick.
Options All -Indexes
or
IndexIgnore *
for more information please check below link
http://viralpatel.net/blogs/htaccess-directory-listing-enable-disable-allow-deny-prevent-htaccess-directory-listing/

How to ignore subfolders and redirect to the root directory with .htaccess

I'm new in the world of .htaccess and I have a problem. I searched in google how to redirect always to a file, like wordpress do it, and I found this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /demo/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /demo/index.php [L]
</IfModule>
I tested this and it works but I want to ignore the subfolders and redirect like it does when you request a folder which doesn't exist. Does anyone how to ignore all the subfolders, and redirect without changing the URL?
Remove the second RewriteCond which checks if directory doesn't exist !-d
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /demo/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /demo/index.php [L]
</IfModule>

.htaccess redirect certain virtual directories

/podcast/wp/ is a folder, everything else is a virtual directory already generated by RewriteEngine. Here's the code provided by WordPress:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /podcast/wp/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /podcast/wp/index.php [L]
</IfModule>
I would like to redirect all requests in the wp/ directory (except for existing folders)
excluding the following possible paths (also virtual directories):
/podcast/wp/ANYSTRING1/ANYSTRING2/feed
to another domain:
example.com
using .htaccess while the excluded path remains working as is.
The goal is to "hide" (redirect) the entire WordPress blog except for the feeds.
Thanks for your help!
Change the wordpress generated rules to:
RewriteEngine On
RewriteBase /podcast/wp/
# new stuff
RewriteCond %{REQUEST_URI} ![^/]+/[^/]+/feed$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://example.com/ [L,R]
# original wordpress stuff
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /podcast/wp/index.php [L]
Depending on how you want to handle the redirect, you can tweak the rule that redirects to http://example.com/. If you want 301 permanent redirects, add a 301:
RewriteRule ^(.*)$ http://example.com/ [L,R=301]
If you want to preserve the relative URI in the redirect, use a backreference:
RewriteRule ^(.*)$ http://example.com/$1 [L,R]
If you want to preserve the entire URI (including the /podcast/wp/ part, use the URI:
RewriteRule ^(.*)$ http://example.com%{REQUEST_URI} [L,R]

.htaccess and subdomains

Hey Folks am seemingly having problems with .htaccess files.
The problem:
I have a wp install in the root directory. In a sub-directory (not a domain) there is Modx. When I enter the url to the root/modx the url is treated like a wp link and it goes back to the homepage. In other words I think the .htaccess is overriding.
The main directory (wp) has this .ht file (after digging around for a solution)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^office/.*$ - [PT]
# BEGIN WordPress
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
</IfModule>
This is the one in the subdirectory
RewriteEngine On
RewriteBase /cms
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
well you could add the following rule to your wp .htaccess just before the RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} !modx

Resources