how to rewrite wp-admin url to some other folder using htaccess? - php

I tried to rewrite a simple RewriteRule but i guess it doesn't turn up good. I created a htaccess file in wordpress wp-admin folder and i tried to rename the wp-admin directory name in my localhost.Here is the code i tried
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^wp-admin$ hello
Just for testing i had done this in my localhost. I want to start from simple pattern not complex pattern so i tried other simple pattern wp-admin and replacement as hello
RewriteEngine On # Turn on the rewriting engine
RewriteRule wp-admin hello
When i go to http://localhost/my_site/hello i can't able to direct to wp-admin for this am getting 404 error. When i try http://localhost/my_site/wp-admin am getting internal server error.
I think the rewrite rule is not good in my try. I have one more doubt should place the .htaccess file in the wp-admin directory or in the my site root directory in my_site or should i place in every directory which am trying to change the folder name on the fly.
What wrong am i doing here.Any help please?

RewriteRule ^hello(.*)$ wp-admin$1 [QSA]
Will change:
http://yourdomain.com/yourwp/hello
http://yourdomain.com/yourwp/hello/path/in/admin
to
http://yourdomain.com/yourwp/wp-admin
http://yourdomain.com/yourwp/wp-admin/path/in/admin
The QSA flag is useful for compatibility.

Here is my wordpress .htaccess with only one custom redirect, but you can understand the idea:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^login/?$ /wp-login.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Related

Add Subdirectory to WP-CONTENT URL using HTACCESS in Wordpress Multisite

I'm in the process of transitioning my single site WordPress installation into a multi-site. I'm trying to fix the broken CSS/JS from my main site.
I currently have two sites on my network:
http://www.example.com (primary)
http://dev.example.com (secondary)
My multi-site installation is inside of a subdirectory we will call "wordpress". So the file path looks like public_html/wordpress.
My goal is for neither site to have the "wordpress" subdirectory in the URL. Everything seems to be working except for broken CSS and JS on the primary site (the secondary site looks fine).
When inspecting the code, all of the CSS and JS calls point to http://www.example.com/wp-content/ but the files are not found there. The files will be found if I go to http://www.example.com/wordpress/wp-content in my browser. I want to hide the wordpress folder and still be able to retrieve the files.
I'm confused on how to setup the HTACCESS file. I already made some initial changes to it in order to get the multi-site within the subdirectory working. These were all following guides I found on StackOverflow and elsewhere online in regard to how to move your site into a multi-site with subdirectory and hiding the subdirectory. I haven't found anything about addressing the broken CSS/JS issue.
I figured I need to make updates to one or more of 3 HTACCESS files.
1.) public_html/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/wordpress/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /wordpress/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ /wordpress/index.php [L]
</IfModule>
2.) public_html/wordpress/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
</IfModule>
3.) public_html/wordpress/wp-content/.htaccess
This file didn't exist but I created it. My thinking was that files are being called without the wordpress subdirectory but they need to act like they have the subdirectory included in them. For example, currently http://www.example.com/wp-content/uploads/image.jpg is broken but http://www.example.com/wordpress/wp-content/uploads/image.jpg works. I want it to be the other way around or I want both paths to work.
<IfModule mod_rewrite.c>
RewriteEngine on
# ADD WORDPRESS IF URL DOES NOT HAVE IT
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/wordpress/
RewriteRule ^(.*)$ /wordpress/$1
</IfModule>
I've tried adding different lines to the various HTACCESS files but none of them worked. I also not sure what line number I should insert a new rule. It's possible that one of my new rules is correct but it is in the wrong place. Below is one that I really thought would work but didn't.
RewriteRule ^/wp-content/(.*)$ /wordpress/wp-content/$1 [R=301,NC,L]
First, you should remove all the .htaccess files and keep only the one in the root: public_html/.htaccess
Second, your last rule isn't working because is slightly wrong.
You should change it from:
RewriteRule ^/wp-content/(.*)$ /wordpress/wp-content/$1 [R=301,NC,L]
To:
RewriteRule ^wp-content/(.*)$ wordpress/wp-content/$1 [L,NC]
Because you don't need the starting / and you don't need to 301 redirect. You want to keep your wordpress folder hidden and just map the requested URLs from wp-content/(.*) to wordpress/wp-content/$1
Also, this rule must be the first in your .htaccess file to have priority over following default Wordpress rules. Your final and only .htaccess from public_html/ should look like this:
RewriteEngine On
RewriteBase /
RewriteRule ^wp-content/(.*)$ wordpress/wp-content/$1 [L,nc]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I hope it helps you.
You shouldn't have to set up several .htaccess files in your sub-directories.
The only .htaccess file that need to be modified is the one located in the application root directory. In your case, it seems to be your public_html/.htaccess or your public_html/wordpress.
Now by default WordPress generates an .htaccess file in that directory which looks something like the following:
# 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
In order to rewrite your URLs, you need to add your RewriteRules before the WordPress code block.
Since RewriteRules are processed from top to bottom, if the request is first rewritten to index.php by the WordPress block, then your rule will never be processed.
Therefore your RewriteRule:
RewriteRule ^/wp-content/(.*)$ /wordpress/wp-content/$1 [R=301,NC,L]
should be enough if it's placed at the top of the root directory's .htaccess, and remove the .htaccess file in the sub-directories
Most Multisite networks are installed in the root directory of your site. This means that if your server is using example.com, then this will be the URL for your base site on the network.
If you’ve installed WordPress Multisite in a subdirectory, then you can’t use subdomains.
If you already have a single site installation at example.com, and you add another WordPress installation in a subdirectory running Multisite, then its address will be example.com/wordpress.
Any site you create on your new network will be at example.com/wordpress/my-new-site. Creating a subdirectory would be impossible here, as it would have to be at an address like example.com/wordpress/my-new-site.network Which just doesn’t work.

Running POST request to php script which sends contact mail from within wordpress subfolder results in 404 Not found

I've just read so much about this issue, but I'm lost here. Can't make this s*$%& work. Here's the issue. Graphic designer has a working WordPress, of which I know same as nothing, about some kindergarten in the city. She builds a landing page to throw a marketing campaign within a subfolder landing201812 inside the public_html folder of the hosting space. The WordPress is installed/deployed/whatever at the root of the site.
Site: https://www.newmills.com.ar/
Landing: https://www.newmills.com.ar/landing201812/
Directories:
public_html/
- lots_of_wordpress_rubbish
- .htaccess
- landing201812/
- sendform.php
Reading about wordpress bootstrapping way for processing all the requests, I have altered the .htaccess file in public_html so it excludes the rewriting of wordpress and goes straight to the landing site.
Original content:
# 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
Actual content
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^landing201812/?.* - [L] # My added rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
With both configurations, the static content (html/css/media) is served correctly, but, any php I would like to execute within that landing201812 folder, fails with a 404 Not Found error. I have tried also to exclude any landing201812 route with RewriteCond directives from the last RewriteRule like
RewriteCond %{REQUEST_URI} !.*landing201812.*
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
But the results are the same.
I am quite limited to move around the wordpress since I know nothing about wordpress. I have considered moving it to a subfolder of the public_html, but I'm afraid of breaking it, or its URLs. I have tried to disable the permalinks, but there's no On/Off switch that I could find.
Last, but in case you may wonder, I have tried including another .htaccess file inside my landing201812 folder. RewriteEngine Off did not work as much as the alternative I took from a native wordpress subfolder:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /landing201812/
RewriteRule ^.*\.php$ - [L]
</IfModule>
# END WordPress
So... does anyone know what's wrong, or even better, how to fix this issue? Everyone says on the web that I should be fine with the htaccess rule, or disabling the permalinks, or moving the wordpress to a subfolder and not the root folder. Moving it is the last thing I would like to do, because I won't know how to fix it if it breaks. Am I missing something? Is there any log I should check?
Thanks in advance for any help.
if you are using cpanel, this is not necessary to do in .htaccess
Instead, can you please keep the original wordpress .htaccess and just assign permissions with filezilla to 775 to the folder?
Also, there should be a configuration missmatch, please try re-loading the PHP version in CPANEL (with PHP MANAGER) only to that folder.
If you can provide access to the site in private, maybe I can give you a hand.
It seems to me that if you were to keep the origional WordPress .htaccess and change your file structure to the following it will work at /landing201812/:
public_html/
- lots_of_wordpress_rubbish (not my opinion, just here to match OP)
- .htaccess
- landing201812/
- index.php
The key is naming the file index.php. Otherwise, you can call the file by name like so: /landing201812/sendform.php.

How to change the default site directory

I just setup a Wordpress in my server (www.albertotry.esy.es) and I added via FTP a new site files, but if I try to access the site, I don't see the index.html of the new site but the default Wordpress site.
I'm pretty sure there is a problem with the .htaccess file:
# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /
# 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
What should I change to page that will display when I type "www.albertotry.esy.es"?
The .htaccess file you're using is the default WordPress one (except for that first line, which is a duplicate). It sends everything that is not a directory or file to index.php. If you want to load index.html when accessing the domain root only, then change the rule in question and add another after it:
Old Rule:
RewriteRule . /index.php [L]
New Rule:
RewriteRule !^$ /index.php [L]
The !^$ basically means "any string that is not empty" - in this case, the string is the request URI.
Then add a new rule below that one (you can also add it before, if you like - shouldn't make a difference):
RewriteRule ^$ /index.html [L]
Here, the ! is removed, which changes the meaning to "any request URI that is empty. As a result, requesting http://www.albertotry.esy.es/ will show the index.html file, but making any other request would rewrite to WordPress.
However, I believe that WordPress may check the content of the code between the comments # BEGIN and # END, so not sure if this is bulletproof. However, it does answer your question.

How to add an exception to a mod_rewrite rule (wordpress site)

I have a WordPress website im trying to call a php script that is located on the website the URL is something like this
http://example.com/folder/process.php
The problem is when I try to do this i always get redirected to the themes "not found" page.
This is the htaccess file
# 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
If i put a test.html file in that directory it will work, and show the contents of the .html file. I think it has something to do with the .php extension it doesn't like?
If i call example.com/wp-config.php it finds that file and deosnt show a 404 page..
This Rule says "If request starts with index.php, do not rewrite and stop".
RewriteRule ^index\.php$ - [L]
Immediately before or after that line you could add something like
RewriteRule ^process\.php$ - [L]
To not rewrite requests to that particular file.
Note that you will have to put your file next to Wordpress' index.php for this to work properly.
Change your .htaccess to this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/+folder/process\.php$ [NC]
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Try turning off mod_rewrite altogether in your /folder/ directory by creating an htaccess file in there and simply adding this single line:
RewriteEngine On
It will make it so any rules from the parent directory won't be applied. If the weirdness with PHP continues, you may want to check if there aren't any rules in your vhost/server config or that things like Multiviews are turned off (via Options -Multiviews).
Ok so if I took a default file that came with the WordPress install made a copy of it in the cpanel rename it remove the code and than put my code in it.
The file is found and it works. So im thinking the only thing left it could have been was a permissions problem. The permissions i put for the file when i uploaded it was 777. I know NOTHING about file permissions.
And i have been working on this for 5 hours and im done trying to figure out what the problem was it works..and im good with that.

kohana htaccess and index structure

I am using kohana 3.2
My site is www.mysite.com/best, kohana is installed on best.
I am using the default htaccess file with rewritebase /best
so what is the best way to redirect users that go to www.mysite.com?
Right now if someone puts www.mysite.com/helo (www.mysite.com/best/helo)
It loads but apache gives 404 not found.
I hope my question makes sense.
EDIT
If a user goes to main domain (www.mysite.com) I want it to load kohana located at mysite/best.
but if a user right now types the full thing and omits best, site will load but my forms wont work because it is posting to www.mysite.com/helo not www.mysite.com/best/helo
instead of loading the fake helo controller, it must show a page not found.
EDIT
Ok I added this to htaccess file and it works perfectly
RewriteCond %{REQUEST_URI} !^/best/
RewriteRule ^(.*)$ /best/$1
You can't have multiple rewritebase in htaccess so...
rewrite your rules to fit you need in /best
or put an other htaccess in / to rewrite your needs.
Try this .htaccess instead:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /best/
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
#RewriteRule .* index.php?kohana_uri=$0 [PT,L,QSA]
RewriteRule .* index.php?/$0 [PT,L,QSA]
Note that the trailing / on RewriteBase is required.

Categories