htaccess rewrite rule to Wordpress page without changing URL - php

I have a rewrite rule in my .htaccess file:
(/.htaccess)
RewriteBase /
RewriteEngine On
RewriteRule ^wptest$ ./wp/main/ [L,R]
I've tried lots of combinations of different switches here, no R, P, OR etc, most caused problems.
The Wordpress htaccess file is untouched:
(/wp/.htaccess)
RewriteEngine On
RewriteBase /wp/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
When I enter:
{domain}/wptest
The browser resolves to:
{domain}/wp/main
but always changes the URL to: /wp/main.
The file structure is:
/.htaccess
/wp/.htaccess
(there is no file or directory /wp/main - this is the Wordpress page stub)
How can I prevent the Wordpress htaccess from changing the URL - ie going to the Wordpress page "main", but leaving the URL as /wptest?

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.

Wordpress .Htaccess Usage In Subfolder Site

Could someone help me; where am i doing wrong?
My wordpress site is not in root; i installed it in a directory(newsite/) I mean; there are 2 sites.
My root site(example.com) has default htaccess file.
And this is my sub wordpress site's(example.com/newsite/) .htacces source:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /newsite/
RewriteRule ^newsite/news/hello-world /newsite/news/?title=hello-world [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /newsite/index.php [L]
</IfModule>
# END WordPress
I just want:
www.example.com/newsite/news/hello-world
(above url should open the url at below; without url change at address bar)
www.example.com/newsite/news/?title=hello-world
Check with different permalink options.
Note : .htaccess file should be writable or paste the suggested code in .htaccess after permalink saved.

Redirect everything that has /en/ to a specific page

I would like to have a multilingual website but I haven't finished english version yet so I would like to redirect if url has /en/ to a specific page with a content "Soon". This page would be www.sitename.com/en/welcome
I am doing this in Wordpress and I don't know .htaccess that good. This code actually works pretty good, it checks for language set and redirects accordingly, but the problem is that after visiting that url I want to go to www.sitename.com, it still redirects me to that page (probably since locale doesn't change).
add_action("template_redirect", 'pl_redirect');
function pl_redirect() {
// if is english redirect to page id 193
if (get_locale() == 'en_US' && !is_page(193)){
wp_redirect( 'http://www.sitename.com/en/welcome' );
exit;
}
}
How can I accomplish the same thing correctly?
I tried this in .htaccess but it doesn't work
Options +FollowSymLinks
RewriteEngine on
Redirect 301 ^/en/$ /en/welcome
This is by Wordpress inside .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /web/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php [L]
RewriteRule ^/en/$ /en/welcome [R=301]
</IfModule>
Assuming you have actually installed everything in a directory called /web - you'll need 2 .htaccess files:
/.htaccess (the root WordPress .htaccess file)
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
# the base directory
RewriteBase /web/
# this means "don't rewrite /web/index.php"
RewriteRule ^index\.php$ - [L]
# this is a router any URL that's neither a file
# nor a directory will be routed to /web/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php [L]
</IfModule>
/web/.htaccess (your rewrite)
RewriteEngine On
RewriteRule ^en/?$ /web/en/welcome [NC,L,R=302]

htaccess redirect wordpress to index2.php

We have a site running on WordPress ada.localhost.com
Now all request to base url (http://ada.localhost.com/) have to go through tracking page (track.com/c/0912321323/?u=xxx) where u parameter is where to redirect user after he is tracked.
I've created a copy of index.php (index2.php) and I want to create htaccess rule to redirect all base url traffic to:
http://track.com/c/0912321323?u=http%3A%2F%2Fada.localhost.com%2Findex2.html (http://ada.localhost.com/index2.php)
Typical WP htaccess file looks like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any clue on how to set it up and if WordPress allow this configuration?
From what I found WordPress won't work with index2.php.
Solution for this is to write a plugin which deals with it.

URL redirection in Wordpress manually by editing .htaccess file

please help me to fix my one of wordpress problem with URL redirecting in PHP.
This is my original wordpress htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I want to create custom URL redirection here. For ex.,
I want to redirect pages,
http://example.com/b/1 > http://example.com/b?n=1
http://example.com/b/2 > http://example.com/b?n=2
i am using directory URL type (displaying URLs as directories without extensions) SEO options in wordpress settings.
inside of http://example.com/b page, I have included another php file using ‘include(‘z.php’);’ command.
Z.php is reading URL parameter comes from ‘/?n=1’ through redirected URL.
I tried below solution, but no luck..
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^b/([^/]*)$ /b/?n=$1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Appreciate any of help.
EDIT
wordpress header file can read the translated parameters. i checked that by displaying GET parameter 'n' in title.
but it shows file not found for http://example.com/b/
i verified that, by editing http://example.com/b/ page content. non of page content displaying after rewriting.
Any help appreciated
EDIT
URL may have characters in parameter as below.
http://example.com/b/abc_1 > http://example.com/b?n=abc_1
http://example.com/b/aa_2 > http://example.com/b?n=aa_2
Thanks for Helping
At first sight it seems to me that it should be
RewriteRule ^b/([^/]*)$ /?p=$1
Wordpress doesn't know what to do with the url /b?n=X.
If you want http://example.com/b/abc_1 to work, the abc_1 part must match the slug of a wordpress post.
Additionally you have to change the settings->permalink structure to match either postname or a custom structure like /b/%postname%.
http://example.com/b/1
to
http://example.com/b?n=1
Using this
RewriteRule ^b/([0-9]+)/?$ b?n=$1 [NC,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^b/([0-9]+)/?$ b?n=$1 [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Test hear
More info
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^b/([^/]*)$ /b/?n=$1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#add below line
RewriteRule ^b/([0-9]+)/$ http://example.com/b?n=$1 [L]
</IfModule>
I found the word press related solution for this question.
in word press all the requests are heading to index.php automatically. if we create custom rewrite to another page, that request not going through index.php. that makes wordpress unidentified request and wordpress will return page not found message.
becasue of that, we have to redirect custom rewrite to index.php instead of permalink.
i used
RewriteRule ^b/([^/]*)$ index.php?page_id=4&n=$1
this rule redirected all requests matched with rule to index page. (my page ID for /b/ is 4)
this worked successfully for me.
other solutions above worked for non wordpress situations. but i found wordpress behavior as i explained.
appreciates who tried to answer.

Categories