Apache rewrite rule is not taking effect after restart - apache

I have these in my Apache vhost
RewriteEngine on
RewriteCond %{QUERY_STRING} ^highlight=WyJwb3J0YWwiLCJub3RpY2VzIiwicG9ydGFsIG5vdGljZXMiXQ==(&.*)?$ [NC]
RewriteRule ^component/k2/item/473-careers-portal-notices\.html$ /career-portal-notices.html?%1 [R=301,NE,NC,L]
However, when I hit site.com/component/k2/item/473-careers-portal-notices.html?highlight=WyJwb3J0YWwiLCJub3RpY2VzIiwicG9ydGFsIG5vdGljZXMiXQ== I do not get redirected to /carerr-portal-notices.html.
What do I need to change to make this redirection happen? And I've restarted Apache already.

This is a Joomla site. Apparently the .htaccess file that comes with Joomla can override any rewrite rules you place in the vhost.

Related

Redirecting and changing URI using .htaccess

I have requests like https://example.net/files/public/file.html which I would like to redirect to https://example.com/domain/public/file.html via htaccess.
In theory I would have to write an if condition and then remove the files part from the URI and then redirect to the new domain. But in practice my code doesn't work.
Has anyone a working example for this type of scenario?
Cheers
In site root directory of example.net you can use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.net$ [NC]
RewriteRule ^files/(.*)$ http://example.com/domain/$1 [L,NC,NE,R=301]
References:
Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details
Apache mod_rewrite In-Depth Details

.htaccess rules being ignored

I have the below in a .htaccess file. The SSL redirect is working perfectly, however when I got to abc.html I get a 404. The test.html page exists and works if I go directly to it.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^abc\.html$ /test.html [L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.org/$1 [R,L]
Rewrite rules were all working as expected with a much longer list of rules previously. After updating the primary domain in cPanel they stopped working and have reduced it down to this minimal example.
Appears there was a configuration error in the httpd.conf generated by cPanel when the domain was updated. Forcing cPanel to regenerate the conf files fixed the issue.

convert nginx redirect to .htaccess apache (drupal 7)

I have to redirect url in .htaccess:
original: /mobile/blog/
redirect : /blog/
I tried:
RewriteCond %{REQUEST_URI} ^/mobile/blog/
RewriteRule ^mobile/blog/(.*)$ /blog/$1 [R=301,L]
Without success.
I aslo have these nginx redirect that need to be in .htaccess.
rewrite ^/items/(.+)/(.*) /search/$2 permanent;
rewrite ^/items/(.*) /search/$1 permanent;
rewrite ^/topic/onlyon $scheme://$host/tags/onlyon permanent;
Any help and documentation is appreciated.
Since Drupal sometimes updates the .htaccess file and Drupal often maintains a set of redirect internally it often makes sense to handle redirects using the Redirect module.
That said, if you need or want to do redirects in the .htaccess you can do that just fine. The first redirect you can do without rewrite, you can use Apache's redirect directive:
Redirect permanent /oldlocation /newlocation
To move everything in a directory using rewrite you can use something like this:
RewriteRule ^items(/.*)?$ /search/$1
See also: Apache rewrite rule for whole directory

Apache mod_rewrite - want a file system redirect not a 301 redirect

I have example1.com on a shared web host running Apache. It has a directory example1.com/foo. I now want example2.com to serve the same content from example1.com/foo, except at the example2.com root without the intervening directory in the URL. Like example2.com/bar.html should serve the same content as example1.com/foo/bar.html .
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule ^(.*)$ foo/$1 [NC]
This simple rewrite rule takes any request intended for example2.com and inserts the foo/ to point to the content which is in that directory. Problem is this keeps doing an external 301 redirect. I don't want that, I want the browser to stay on example2.com without redirecting while Apache serves up the content from /foo in the filesystem.
Been over the Apache mod_rewrite docs several times, which say how to force a 301 redirect with the [R] flag but don't say how to force it NOT to happen. What am I missing here? It is behaving the same on both my Linux shared host and a local test with Apache on Windows.
Thanks!
I figured this out. The 301 was happening because I had the directory name wrong in the rule. So the result of the rule pointed to a path that didn't exist, which makes Apache try to fallback from the file system redirect to a 301 redirect.
Then I had to fix an infinite loop, since that above rule always adds "foo" to the URL even if it's already present so I'd get foo/foo/foo/foo/... . We need to add it only if it's not already there. Had to do it with this two-step rule, because you can't use wildcards in a capturing group of a negative rule. But this seems to work, adding "foo" when the host is example2.com and the URL does not already contain "foo".
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule !^foo - [C]
RewriteRule ^(.*)$ foo/$1 [NC,PT]

.htaccess require SSL for a particular URL

I want to force Apache to use HTTPS for a particular URL in the following form:
https://www.example.com/signup/*
so
if someone goes to any of the following example URLs directly, Apache will forward the URL over to the HTTPS equivalent site.
e.g.
http://www.example.com/signup --> https://www.example.com/signup
http://www.example.com/signup/basic+plan --> https://www.example.com/signup/basic+plan
http://www.example.com/signup/premium --> https://www.example.com/signup/premium
Anyone know how?
Thanks in advance
Thank Murat,
Yours almost worked but figured out how to get it to exactly work.
The following is what works:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/somefolder/?
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
Notice that I didn't include somefolder in the www.domain.com rewriterule
I think this was what i used:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/somefolder/?
RewriteRule ^(.*)$ https://www.domain.com/somefolder/$1 [R,L]
(from here)
You can use the Redirect directive:
Redirect 301 /signup https://www.example.com/signup
This will automatically preserve anything following /signup in the URL. Be sure to configure this directive only on your non-SSL site, or it might get into a recursive loop!
You should take a look at mod_rewrite documentation
I used the following to require the checkout section of a website to require SSL:
<Directory "/var/www/html">
RewriteEngine on
Options +FollowSymLinks
Order allow,deny
Allow from all
RewriteCond %{SERVER_PORT} !^443$
RewriteRule \.(gif|jpg|jpeg|jpe|png|css|js)$ - [S=1]
RewriteRule ^checkout(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</Directory>
So for example, hitting http://www.example.com/checkout redirects to https://www.example.com/checkout
The rule will skip file extensions that are typically included within a page so that you don't get mixed content warnings. You should add to this list as necessary.
If you want multiple pages change the RewriteRule to something like:
RewriteRule ^(checkout|login)(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
Of course, the directory should match the actual path on your server. This page may also help with some more information for your specific needs: http://www.whoopis.com/howtos/apache-rewrite.html
I'm using this on a website that runs Plesk 8.6 but that shouldn't matter. This is in my vhost.conf file which is like putting it in your httpd.conf file. I'm not sure if you'd need to adjust anything to use it in a .htaccess file but I doubt it. If adding to a conf file don't forget to restart apache to reload the configuration.
If you are like me and want to use SSL only on particular pages then you also want a rewrite rule that sends you back to regular http for the rest. You can use the following for the reverse effect:
RewriteCond %{SERVER_PORT} ^443$
RewriteRule \.(gif|jpg|jpeg|jpe|png|css|js)$ - [S=1]
RewriteRule !^(checkout|login)(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [L,R]
If you are using Plesk like I am keep in mind that all non-SSL traffic uses the vhost.conf file but all SSL traffic uses the vhost_ssl.conf file. That means your first rewrite rule to require SSL would go in the vhost.conf file but the second rule to force back to non-SSL will have to go in the vhost_ssl file. If you are using httpd.conf or .htaccess I think you can put them both in the same place.
I've also posted this tutorial on my blog: Apache rewrite rules to force secure/non-secure pages.
You can do this with mod_rewrite -
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/signup https://example.com/signup
RewriteRule ^/signup/(.*)$ https://example.com/signup/$1
Should work, though I haven't tested it.
-- edit --
Correction, I just tried this on one of my servers, and it works fine for me. You may want to doublecheck your mod_rewrite configuration. Also, if you're using .htaccess, you'll want to make sure overrides are allowed for that directory.
As a side note, this assumes your SSL traffic is coming over port 443. If it isn't, you'll need to adjust the rewrite condition accordingly.
.htaccess files are normally placed in a scope with Options -FollowSymLinks, which blocks Rewrite rules. This is often a security rule.
So a more trivial thing is often needed like this one:
<If "%{HTTPS} != 'on'">
Redirect 301 /your/path https://www.example.com/your/path
</If>
This is a small enhancement to the answer of Greg Hewgill.