htaccess file to hide subfolder - .htaccess

I'm struggling with the .htaccess file. I've wordpress installed in a subdirectory 'wordpress'. In the root folder if have the htaccess with the following content:
Order Allow,Deny
Allow from all
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule !^wordpress/ /wordpress%{REQUEST_URI} [L,R=301]
</IfModule>
Redirection is working, but how can I hide the subfolder 'wordpress'?
THX in advance
EDIT: Tried following content now but still not working:
root htaccess:
Order Allow,Deny
Allow from all
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule !^wordpress/ wordpress%{REQUEST_URI} [L]
</IfModule>
wordpress htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
EDIT2: my whole root htaccess looks like this now:
Allow from all
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule !^wordpress/ wordpress%{REQUEST_URI} [L]
If i type in www.example.com I am redirected to example/wordpress/home, example/wordpress/contact and so on...
I would like to hide the wordpress directory like example/home, example/contact and so on

Redirection is working, but how can I hide the subfolder 'wordpress'?
You shouldn't be "redirecting". You should be internally rewriting the request instead. Remove the R flag.
For example:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule !^wordpress/ wordpress%{REQUEST_URI} [L]
The slash prefix on the susbtitution string is also not required.
This assumes you have the standard WP .htaccess file in the /wordpress subdirectory.
UPDATE: Also confirm you have removed the /wordpress subdirectory from the "Website Address" and "Site Address" in WordPress General settings.

Related

.htaccess issue with url

This url format https://portal.com/pages does not work but this https://portal.com/index.php/pages works.
I want to be able to remove the index.php from the url.
My root .htaccess file is
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
# RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
While public folder .htaccess file is
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
Try following rules htaccess file. Make sure your index.php is present inside public folder. Please make sure to clear browser cache before testing your URLs.
root htaccess rules file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{REQUEST_URI} !^/public [NC]
RewriteRule ^(.*)/?$ public/$1 [L]
</IfModule>
Public folder inside htaccess Rules file:
<IfModule mod_rewrite.c>
RewriteOptions InheritBefore
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/?$ index.php?url=$1 [QSA,L]
</IfModule>
Fixes done in OP's attempt:
root htaccess Rules file didn't have https apply rule, so have added it.
Made public applying rule for all kind of urls.
Then in public folder rules file added InheritBefore option to make sure root's file is being inherited to public folder's htaccess rules file.

htaccess to Laravel public folder using https ONLY

My Laravel project is already inside in shared hosting server(i know what do you thinking...). And inside root folder i got a .htaccess file which has this code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
# For all files not found in the file system, reroute the request to the
# "index.php" front controller, keeping the query string intact
<IfModule mod_rewrite.c>
RewriteCond $1 !^(public)
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
This gives me good URL's without "public" but there is one case:
If i go:
www.example.com
so it always stays with URL www.....If i go:
https://www.example.com
it always stays with URL https://....
My question:
How to make that the URL would always be https://www.example.com whatever I enter www or https first?
This should do:
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You can do that by putting following lines in the .htaccess files
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^.*$ https://%{HTTP_HOST}/public%{REQUEST_URI} [L,R=301]
</IfModule>
Put the code in .htacces in your laravel root directory, it should do the job:
RewriteEngine On
# enforce https
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
# use public directory as root, but don't include it in url
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]

cake .htaccess rewrite almost working

I'm having a problem with rewrite rules in my cake 2.2.0 app.
Mod_rewrite seems to be working as going here:
myapp.com/listings works
But links generated by cake construct this type of URL, which also work but are not being rewritten:
myapp.com/app/webroot/index.php/listings
I've tried a lot of posted options but nothing seems to work?
My apache config is set up as follows:
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
All .htaccess files are standard from the cake install?
Webroot .htacess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
App .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
root .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Should I be looking at anything else?
This is something you should be able to fix in cakephp when you generate your links. The htaccess files that you have won't change the links in your page content.
If for whatever reason, you can't do this in cakephp, then you can add this to your Webroot htaccess, right below the RewriteEngine On line:
RewriteCond %{THE_REQUEST} \ /app/webroot/index.php/([^\ ]+)
RewriteRule ^ /%1 [L,R=301]
and that will redirect the browser to the shorter URL.
In case any one else looks at this, my problem was that this line in /app/Config/core.php was uncommented:
Configure::write('App.baseUrl', env('SCRIPT_NAME'));
Commenting it out removed the abs URL /app/webroot/index.php/ from my URLs.
Happy coding.

I need a Wordpress folder to NOT be case sensitive without writing 200 redirects

I have a blog set up at blog.ftj.com/ACSM, it is hosted with Bluehost and their folder structures seem to be case sensitive. Is there something in the .htaccess file that I can adjust so that all possible combinations get redirected to the specific uppercase URL.
Another issue is that it seems that I need to redirect
blog.ftj.com/acsm/
with and without the forward slash.
Here is my current .htaccess file
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ACSM/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /ACSM/index.php [L]
</IfModule>
# END WordPress
Please submit the full change if you would.
You need to place the following .htaccess in the root dir to rewrite all requests to /ACSM into /acsm
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/acsm$ [NC]
RewriteRule ^(.*)$ /ACSM [L]
RewriteCond %{REQUEST_URI} ^/acsm/(.*)$ [NC]
RewriteRule ^acsm/(.*)$ /ACSM/$1 [L]
</IfModule>
Sorry for delays, have not got an Apache at hands....

How do I get two htaccess rewrite rules to 'play nice' together?

In my website root I have the following to redirect to non www domain
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^www\.notails\.com [NC]
RewriteRule ^(.*)$ http://notails.com/$1 [R=301,NC]
in a subfolder named 'photography' I have this...
RewriteEngine on
RewriteRule ^show/([^/\.]+)/([^/]+)$ show.php?section=$1&photoid=$2 [L]
Anything inside the photography folder ignores the www removing rule. How do I get these two rules to both apply to folders/files within the photography folder?
Also... my root 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
Could it be interfering? I have a self-hosted wordpress blog but it's not in the root of the website, it's in a subfolder called 'blog' so I don't know why this rule is in my root's htaccess file. Can/should I move it?
Edit: Just to point out, in case it isn't obvious - I'm a complete noob when it comes to htaccess and mod_rewrite stuff. Does a htaccess file in a subfolder override any htaccess files nearer to the root than it? Or do the htaccess contents combine?
Edit 2: I have tried moving the second rule to the same htaccess file as the www removing rule as per the following code...
# 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
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^www\.notails\.com [NC]
RewriteRule ^(.*)$ http://notails.com/$1 [R=301,NC]
RewriteRule ^photography/show/([^/\.]+)/([^/]+)$ photography/show.php?section=$1&photoid=$2 [L]
If I then go to one of my photography pages it resolves to the intended url (http://notails.com/photography/show/pointofayre/260 for example) but the page is a 404.
If I manually add 'www' to that it undoes the other rule... (http://notails.com/show.php?section=pointofayre&photoid=260) and removes 'photography/' from it.
Add RewriteOptions inherit into .htaccess in your "photography" folder because right now all rewrite rules from parent folders are ignored (default behaviour).
Alternatively move rewrite rules from that subfolder .htaccess into root one (you will need to slightly modify your rule by fixing the path -- adding photography/ may be enough, depends on actual "photography" location)
UPDATE:
Your root .htaccess can be like this:
Options +FollowSymlinks
<IfModule mod_rewrite.c>
# activate rewrite engine
RewriteEngine On
# we are in the root
RewriteBase /
# no www please
RewriteCond %{HTTP_HOST} ^www\.notails\.com [NC]
RewriteRule ^(.*)$ http://notails.com/$1 [R=301,L,QSA]
# photography
RewriteRule ^photography/show/([^/\.]+)/([^/]+)$ /photography/show.php?section=$1&photoid=$2 [L]
# WordPress rules
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Resources