Rewrite rule for opening a different page - apache

I'm stuck with some .htaccess rules.
I've got the page /module/slug. I want, when I type slug to see content of /module/slug/ but not redirect to this page. Is it possible? I've tried this rule, but had no luck:
RewriteRule ^slug/$ /module/slug/ [L]
My .htaccess 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]
RewriteRule ^calculator/$ /sample-page/ [R=301,L]
</IfModule>
# END WordPress

That happens because you don't tell Apache to redirect (R=301).
Add the redirect flag R, and it should work. Also make sure that:
You have set the RewriteBase correctly.
There are not any other rules earlier in the file that match the same URL.
.
RewriteEngine On
RewriteBase /
RewriteRule ^slug/$ /module/slug/ [R=301,L]

Related

.htaccess 301 redirect with exclusion does not work

I try to use a simple 301 redirect
from domain1.com/folder/ to domain2.com/
but excluding domain1.com/folder/subfolder
I use the following code in .htaccess:
RedirectMatch 301 ^/folder/((?!subfolder).*)$ https://domain2.com/$1
but it simply redirects all the requests, including the requests to subfolder.
Please, help to fix the line to make it work as described. Thank you!
here is the complete code of .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]
</IfModule>
RedirectMatch 301 ^/folder/((?!subfolder).*)$ https://domain2.com/$1
Try it like this using mod_rewrite instead:
(NB: This assumes the .htaccess file is located in the document root.)
# /.htaccess
# Redirect all direct requests, except "subfolder"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond $1 !^subfolder($|/)
RewriteRule ^folder/(.*) https://domain2.com/$1 [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]
</IfModule>
It is important that the redirect goes before the rewrite to your front-controller.
You will need to ensure your browser cache is cleared before testing and test with a 302 (temporary) redirect to avoid potential caching issues.
UPDATE:
Yes, /folder has it's own .htaccess (this is the file I am working at all this time). Yes, /folder is where Wordpress is installed.
In that case you would need to change the above redirect to read as follows (it won't do anything otherwise):
# /folder/.htaccess
# Redirect all direct requests, except "subfolder"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond $1 !^subfolder($|/)
RewriteRule (.*) https://domain2.com/$1 [R=301,L]
Basically, you need to remove folder/ from the start of the regex that matches the URL-path. The URL-path that the RewriteRule pattern matches against is relative to the directory that contains the .htaccess file.
The addition of the check against the REDIRECT_STATUS env var is to ensure that rewritten requests to the WP front-controller (when subfolder is requested) are not redirected.
You can also "simplify" the WordPress directives that follow (although if these are enclosed in # BEGIN WordPress / # END WordPress comment markers then you should leave the directives as they are since they are maintained by WordPress). For example:
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
The RewriteBase directive is not required. And neither is the <IfModule> wrapper. (But as I said above, only change this if you are hand-coding the .htaccess and not letting WordPress maintain it.)

Apache .htaccess RewriteRule not including subdirectory when redirecting non-www to www url

I've setup a rewrite rule in order to direct domain.com traffic to www.domain.com using the following rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
From my limited understanding, the $1 in the RewriteRule should match anything after domain.com/ and then be placed at the end of my rewritten url www.domain.com/.
For example domain.com/abc should become www.domain.com/abc.
However this is not working. If I browse to domain.com/abc then this path isn't rewritten at all and I just get domain.com/abc without the www.
I've done lots of reading just to figure out that the $1 should be taking care of this, from my understanding.
Can anyone explain why it isn't working as I suspect it should be? Thanks.
It turns out that because I was using Wordpress AND W3 Cache plugin that I was simply putting the code in the wrong place in my .htaccess file.
The code should be placed within the # BEGIN Wordpress codeblock and before the existing rules that Wordpress already has in place.
The existing code should look something like 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
I added my original code to this as follows:
# BEGIN Wordpress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#END Wordpress
This fixes the issue I was describing above. Thanks to the commenters for sparking my thought process.

Rewrite only root without redirect not working

I have a problem that I can't get to rewrite only root to file. For example example.com to example.com/landing-page/mypage.html but keep all other addresses like example.com/something/ intact.
Currently for test purposes I have managed to redirect /test/ and my .htaccess looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/test/.*$
RewriteRule . /wp-content/themes/mytheme/landing_page.html [L]
# 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
But now if I try to redirect only root and replace RewriteCond with
RewriteCond %{REQUEST_URI} ^/$
it doesn't work and it loads my Wordpress site.
UPDATE:
I tested on another server using just simple rule RewriteRule ^/?$ /mypage/landing-page.html [L] also keeping WordPress lines and it works there, but the same lines do not work on this server. So now the question arises what server settings might be preventing this simple rule.
Looks like you forgot to turn on the RewriteEngine in your second example .
RewriteEngine On
RewriteRule ^$ /landing-page/mypage.html [NC,L]

PHP Rewrite SEO friendly URL - Inside folder

This a php mod rewrite related question
Background info :
I am using wordpress site in my root ( example.com)
I have a folder created under that called 'search-jobs', which has all the php code ( example.com/search-jobs) I am not using wordpress for anything on this search page, but wanted to ensure you have that info and if it impacts.
I am trying to rewrite the below URL :
http://example.com/search-jobs/?searchText=FACEBOOK+INC.&searchCity=Enter+US+City+or+Zipcode&searchYear=14&action=search&searchJobTitle=Enter+Job+Title+%2F+Role+Name
I have the below mod rewrite written in the .htaccess file, which is placed in the root folder, where wordpress is installed.
# BEGIN search Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^company/([0-9a-zA-Z_-\s]+)/([0-9a-zA-Z_-\s]+)/([0-9]+)/([0-9a-zA-Z_-\s]+)/([0-9a-zA-Z_-\s]+)$ /search-jobs/?searchText=$1&searchCity=$2&searchYear=$3&action=$4&searchJobTitle=$5 [NC,L]
</IfModule>
# END search Rewrite rules
Also, four of the variables typically have spaces as they are search strings. Not sure, if that is causing issues.
Below is the full Wordpress rewrite rule that excludes the folder
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/(search-jobs|search-jobs/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I saw some wordpress rewrite rules in the .htaccess file, is that messing up my rewrite rules or am I doing something wrong ? I am doing this for the first time. I have tried for couple of days on this reading many other, badly stuck. Any help would be great.
Have root WP .htaccess like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(index\.php|search-jobs(/.*)?)$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Then inside /search-jobs/.htaccess have code like this:
RewriteEngine On
RewriteBase /search-jobs/
RewriteCond %{THE_REQUEST} \?searchText=([^\s&]+)&searchCity=([^\s&]+)&searchYear=(\d+)&action=([^\s&]+)&searchJobTitle=([^\s&]+) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=302,L,NE]
RewriteRule ^([^/]+)/([^/]+)/(\d+)/([^/]+)/([^/]+)/?$ ?searchText=$1&searchCity=$2&searchYear=$3&action=$4&searchJobTitle=$5 [NC,L,QSA]

Remove specific slug in with htaccess

I have a Wordpress site web in localhost with XAMPP. Now I have a URL like this:
http://localhost:82/subdirectory/images.html/nggallery/tags/[tagname]
Now I want this:
http:// localhost:82/subdirectory/images/nggallery/[tagname]
To remove tags, I've tried many codes, like this:
RewriteRule ^tags/(.+)/$ /$1 [R=301,L]
but doesn't work.
Help me, please.
I'm using WordPress, my htaccess file looks like this
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subdirectory/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subdirectory/index.php [L]
</IfModule>
# END WordPress
Before answering, a better idea than using htaccess for this is to use Wordpress' rewrite modules.
Anyway, you need to put your specific rules before Wordpress' main rule.
You can replace your current code by this one in your htaccess (which has to be in your subdirectory folder)
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /subdirectory/
# redirect /.../tags/XXX to /.../XXX
RewriteCond %{THE_REQUEST} /images\.html/nggallery/tags/([^\s\?]+)\s [NC]
RewriteRule ^ images/nggallery/%1? [R=301,L]
# internally rewrite /.../XXX to /.../tags/XXX
RewriteRule ^images/nggallery/(.+)$ images.html/nggallery/tags/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
# END WordPress
try
http:// localhost:82/subdirectory/images/nggallery/[tagname]
RewriteRule ^nggallery/tags/(.*)$ http://localhost:82/subdirectory/images/nggallery/$1