Website homepage to show subdirectory content via htaccess - php

I have a Wordpress website with root located at
http://www.example.com/web
Via htaccess, when I visit that address I would like to see what's on http://www.example.com/web/about-me with the browser still displaying http://www.example.com/web.
Is it possible to achieve this via .htaccess? What I have right now is
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]
</IfModule>
END WordPress
I tried adding at the bottom
RedirectMatch ^/$ /about-me/
Without any success.

So essentially you have a website which is hosted on a sub-directory of your domain, and you want to rewrite the homepage URL to your about-me page, but without the user seeing the URL change. Your main issue is trying to use a Redirect which will change the displayed URL.
This should do it
RewriteBase /web # Dropped the / suffix otherwise the rewrites get //
## RewriteRule ^index\.php$ - [L] - # Removed because the Conditions below should be doing this?
RewriteRule ^$ /about-me [L] # Redirect homepage
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L] # dropped the /web prefix as that is managed by the RewriteBase
Results:

Related

htaccess - add custom rewrite to standard wordpress rewrites

i have a wordpress website
i have uploaded some of my php code at the root of my web where wordpress is installed.
i want to open those page directly.
they open if # www.example.com/newcode.php
but i want to open them like www.example.com/newcode (without extension .php)
the problem is when i put some .htacess code to achieve this, all wordpress pages stops and shows error404 not found and if i remove htaccess code then i cant open my own newcode.php page without extension
i tried to put this code in htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^\.]+)$ $1.php [NC,L]
but doesnt work...
this is my wordpress .htaccess (automatically created by wordpress)
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^\.]+)$ $1.php [NC,L]
</IfModule>
# 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
id have more files like newcode.php and some folder in which there are more code files and i want all of them to open like this
i want both wordpress pages as well as my new code page to open without any extension and i cant put newcode.php in seperate folder and make another htaccess for that specific folder
This is documented pretty clearly - especially in this case where you don't even need any regexp knowledge. Simply replace everything before # BEGIN WordPress with:
RewriteEngine On
RewriteRule /newcode /newcode.php [L]

Apache mod-rewrite .htaccess subdomain redirect issue

I'm struggling to get my .htaccess configured properly to setup a subdomain redirect for specific subdomains. I want to redirect (not mask) a subdomain to a specific page and repeat this configuration multiple times.
I'm hosted on a Google Compute Engine currently using Cloud DNS (used to host on Godaddy in which subdomain management was very simple)
before:
# 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>
after:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^sat.theteachertutors.com$ [NC]
RewriteRule ^(.*)$ /private-sat-act-tutoring/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I tried the config above but the redirect does not work. The target url is: https://www.theteachertutors.com/private-sat-act-tutoring/
Any guidance/tips would be most appreciated. I've search for other examples but can't get this configuration to work at all, ie: http://sat.theteachertutors.com
Thanks
You were close to the solution, change the rewrite rule to this:
RewriteRule ^(.*)$ https://www.theteachertutors.com/private-sat-act-tutoring/$1 [R=301,L]
When you are redirecting to another domain/subdomain you have to use the full URL. Also I use R=301 to mark the redirect as a permanent redirect (by default rules that point to another domain use a 302 redirect).

Extract URL for Redirect

I have setup a website with recipes but in the process of migration OLD urls to NEW urls I'm facing issues with.
https://example.com/recipes/cheesy-baked-tacos/
This is my old URL structure and In a wordpress site I need to extract last part of the URL "cheesy-baked-tacos" and handle that in the template I use for recipes page.
Now when i click on the URL it goes to 404 page even though recipes page has setup.
I tried following htaccess rules but have no success
RewriteBase /
RewriteRule ^recipes/(.*)$ index.php?l=$1
Thanks in Advance.
Full htaccess file looks like this
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^recipes/(.*)$ index.php?l=$1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
you should check your permalink change to starts with id to without query string
dont change htacces file
you just change permalink in new server
https://example.com/recipes/cheesy-baked-tacos/
this is your old url and has permalink set without query string
new url https://example.com/recipes/cheesy-baked-tacos/id=50 just like
thats why your code have error
Managed to fix the issue with 301 Redirect. Here's what i used if it helps someone.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^recipes/(.*)$ recipe/?post_name=$1 [L,R=301,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Redirection of website-want to use /en folder being redirected to post

I encountered a strange behaviour with WordPress website.
I have created /en folder in the root and want to have another WordPress website there.
The problems appears when I try to open mainwebsite.com/en I'm being redirected to mainwebsite.com/english-post
So, WP gives the priority to the post that is found in the main domain and it's slug begins with "en".
I tried changing the folders name to "ren" and the website in the folder than shows up nicely.
I've checked .htaccess file for some redirects and none is set.
I noticed that this is a typical WP behaviour, as I tested on some "bigger" brands that use WP and same thing happens,ie:
https://www.smashingmagazine.com/en redirects to --> https://www.smashingmagazine.com/2014/11/enabling-multiscreen-tracking-with-google-analytics/
http://vanheusen.com/en redirects to --> http://vanheusen.com/products/english_shaded_box_silk_tie/
Have any idea how to solve this?
This is my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(en/)
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Try using this rule in your main .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php - [L]
RewriteCond %{REQUEST_URI} !^/en
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And then you need this rule below in the .htaccess inside en folder
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /en/
RewriteRule ^index\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /en/index.php [L]
</IfModule>
# END WordPress
Let me know how this works

wordpress 301 redirect .htaccess setting on openshift

I have just set up a wordpress blog on openshift.
Here is what i have done.
Created an alias abc.mydomain.com in openshift.
Cname abc.mydomain.com to myexampleapp.rhcloud.com.
Login to myexampleapp.rhcloud.com and change myexampleapp.rhcloud.com to abc.mydomain.com (Settings-general)
Here i want to do a 301 redirect through .htaccess.
What i want to achieve is when I input the following into browser address bar
myexampleapp.rhcloud.com automatically redirect to abc.mydomain.com
myexampleapp.rhcloud.com/hello-world/ automatically redirect to abc.mydomain.com/hello-world/
But it is not redirecting. (input myexampleapp.rhcloud.com/hello-world/ in browser address bar and it changes to abc.mydomain.com/hello-world/ automatically)
I have been searching a lot for the code
Here is what i have put in htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^myexampleapp.rhcloud.com [NC]
RewriteRule ^(.*)$ http://abc.mydomain.com/$1 [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Not working at all. What should be the exact code to make that redirect happen?

Categories