mod_rewrite equivalent of a RedirectMatch rule - apache

I have a RedirectMatch rule in my .htaccess file that works fine, but I'm having trouble coming up with a comparable rule using mod_rewrite.
My goal is to have this URL: mysite.com/anything/print show this page: mysite.com/anything?view=print.
The rule that works to Redirect it is this:
RedirectMatch 301 ^(.*)/print/?$ http://mysite.com/$1?view=print
But now I'd like to change this from a visible 301 redirect to an "invisible" rewrite using mod_rewrite. I have tried many different variations on this (with and without RewriteBase), and none have worked:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/print/? $1?view=print
What am I doing wrong? Mod_rewrite is definitely enabled, and there are functioning Wordpress-based mod_rewrite rules in the same .htaccess file.
UPDATE
Using tips from #Nathan, I now have this. However, I still get a 404 when I visit mypost/print.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)/print/?$ /index.php/$1?view=print [L]
When I append /print/ to a permalink, the WP_Debug plugin indicates the following:
Request: myposttype/mypost/print
Query String: attachment=print
Matched Rewrite Rule: myposttype/[^/]+/([^/]+)/?$
Matched Rewrite Query: attachment=print

If you have typical wordpress rules in the htaccess file, your rules should come before the # BEGIN WordPress block. Otherwise the wordpress rules will stop the rewrite matching before your rules get called.
Also, you should add $ after your regex pattern unless you also want to match something like: http://domain.com/page/print/something/else/here
Lastly, in order for Wordpress to recognize the change in the URL without redirecting the page, you need to append the URL to index.php/ so that Wordpress will use path info permalinks.
E.g.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)/print/?$ /index.php/$1?view=print [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

RewriteEngine on
RewriteRule ^(.*)/print/$ /$1?view=print [L]
or
RewriteRule ^(.*)/print$ /$1?view=print [L]
without "/" in the end of URL string

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.)

Combine mod_rewrite rules

In my .htaccess I've got the usual rewrite rules but I need to add a HTTP to HTTPS rule. If I put the new rule after the existing rules it does not work but putting it before works but then I suspect the vanilla WordPress code does not work.
I've put the rules together by adding the last two lines of the second rule to the end of the first one but that does not work either. I'm not sure what I'm doing!
What is the best way to combine these two?
# 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
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://127.0.0.1/$1 [R,L]
</IfModule>
The ruleset that's responsible for ensuring https is doing an external redirect, while the other one (wordpress) is doing internal rewrites.
It's often a good practice to first put the external redirects (if they don't depend on any previous internal rewrite).
Note the L flag:
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl, or the break command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules.
If you put wordpress rewrite rules before the https ruleset, the rewrite engine will never get to the second ruleset. That's because of the L flag that instructs the rewrite engine to stop processing of any other rules. This reason aside, you don't want to rewrite the request to wordpress' index.php and then ensure that it's https using an external redirect (R flag), right?
So, you might want to try this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# HTTPS:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://127.0.0.1/$1 [R,L]
# WordPress:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Few final notes:
You only need one RewriteEngine on and one IfModule.
It's better to use a 301 redirect for https and www redirects. (R=301)

htaccess redirect subfolder to root

This has been frustrating me and I can't find the same issue, maybe I am just searching wrong. Someone messed up and we have a bunch of Google links to a subfolder that does not exist. So I am trying to do a 301 redirect in htaccess to sort that out.
The problem is I can't seem to get the redirect to work more than one folder deep.
Example. http://example.com/subfolder/someOtherFolder redirects to http://example.com/someOtherFolder just fine.
However http://example.com/subfolder/someOtherFolder/yetAnother stays on the same page and returns a 404.
This is the last variation of my entire .htaccess that returns the above results, nothing I've tried has returned anything but the above.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^sitefiles/(.*)/$ /$1 [R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
</IfModule>
Help? (I hate .htaccess >.<)
The problem is with this rule:
RewriteRule ^sitefiles/(.*)/$ /$1 [R=301]
This rule required a trailing slash on the requested URI
Instead:
RewriteRule ^sitefiles/(.*) /$1 [R=301]
Note that there's also a Redirect directive to do simple Redirects. In your case:
Redirect /sitefiles /
Finally, note that the entirety of that Rewrite block can be replaced with:
FallbackResource /index.php
Thus, the final recommended (best practice) configuration would be:
Redirect /sitefiles /
FallbackResource /index.php
Rather than using Rewrite at all.

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]

How to redirect URLs based on query string?

I successfully mass migrated a Wordpress site to Drupal. Unfortunately in Wordpress, the content URL's were something like www.example.org/?p=123. My domain is still the same, but I want to do a redirect via htaccess as Drupal will not allow URL's to be www.example.org/?p=123. In other words, the content does not have the same URL as it did in Wordpress. For example, the new Drupal URL would be something like www.example.org/content/MyNewPage
I tried this in my .htaccess file and it does not work
Redirect 301 /\?p=375 http://www.example.org/content/MyNewPage
So I tried the below, but it does not work either.
Redirect 301 /\?p\=375 http://www.example.org/content/MyNewPage
Just as a test, I tried the below and it worked.
Redirect 301 http://www.example.org http://www.google.com
I made sure that my Redirect rule is at the top of the list in my .htaccess so it will be evaluated first. How do I fix this?
neither Redirect nor RedirectMatch allow you to specify a query string for the redirect source.
[Source]
You have to use mod-rewrite for redirecting based on query string:
RewriteCond %{QUERY_STRING} ^p=375$
RewriteRule (.*) http://www.example.org/content/MyNewPage? [R=301,L]
You may consider use ModRewrite in your htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^p=345$ [NC]
RewriteRule index.php content/MyNewPage [NC,L,R=301]
</IfModule>
And you also may want to pass the old page id to the new URL concatenated (or maybe by QS?):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule index.php content/MyNewPage-%1 [NC,L,R=301]
</IfModule>