Simple Mod rewrite working with 302 but not without - .htaccess

I have a very simple mod rewrite section in my .htaccess file as below:
# Custom prime redirects
RewriteEngine On
RewriteRule "^best-fixed-deposits" "/prime-deposits" [L,R=302]
# End Custom prime redirects
This works. But, if I remove the R=302, the server acts as if this rule is not there, throwing a 404 error.
I have this in the top of the .htaccess file.
Any clues?
thanks!

Related

how to put a pattern based generic redirect in .htaccess

I have 2000+ old URLs like
example.com/hello/public/news
example.com/hello/public/notifications
example.com/hello/public/results
now I have trimmed hello/public this part in the new version
so how do I write a generic redirect in .htaccess so if I get hit on these
it redirects to
example.com/news
example.com/notifications
example.com/results
and more ...
I suspect you will already have some mod_rewrite directives in your .htaccess file, in which case you do not need to repeat the RewriteEngine On directive.
Try the following at the top of your .htaccess file:
RewriteRule ^hello/public/(.*) /$1 [R=302,L]
Test first with 302 (temporary) redirect before changing to a 301 (permanent).

Wildcard RewriteRule for 301 redirects in htaccess

I'm working on setting up a 301 redirect in an .htaccess file and I can get it to sorta work, but I'm not sure why it's not picking up on anything after a specific URL in the rewrite rule.
I currently have this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/old-path/page/(.*) /new-page/ [R=301,L]
</IfModule>
Which works fine if I test it with http://example.com/old-path/page which redirects to http://example.com/new-page
But if I try this:
http://example.com/old-path/page/test it will 404 and not redirect to http://example.com/new-page
Overall, since I'm not 100% if there are any other pages from the old path that may be nested within the page I want to make sure that I'm catching any of them and just redirecting to the new-path outright.
So, I'm not sure if I'm setting this up incorrectly or if there is something that I'm missing?

Redirect site to 410 except one URL

I have site build in PHP codeigniter which has various URLs as follows:
www.mysiteexample.com
www.mysiteexample.com/mobile
www.mysiteexample.com/storage
www.mysiteexample.com/stack
www.mysiteexample.com/apple and so on..
Now I want to redirect all the URLs to 410 gone including homepage but except www.mysiteexample.com/stack I am able to do this by adding separate rules for each URL as follows:
RewriteRule ^/?mobile- [L,R=410]
RewriteRule ^/?storage- [L,R=410]
RewriteRule ^/?apple - [L,R=410]
But I want optimized solution for because I have more than 50 URLs which I want to redirect to 410 and using above solution it increases file size and LOC of .htaccess
Edit:
I have following code in routes.php
$route['stack(.*)'] = 'stack_center/index';
Try below rule too, for now I didn't tried it.
RewriteEngine On
RewriteCond %{REQUEST_URI} !stack
RewriteRule ^ - [L,R=410]
You can use the following Redirect
RedirectMatch 410 ^/((?!stack).*)$

Can I remove the .html from .htacces just for a single page?

I've got a website with the following links
www.mydomain.com/NiceUrl1.html
www.mydomain.com/blog.html
www.mydomain.com/AnotherNiceUrl.html
What .htaccess rewrite rule, should I apply to achieve the following result?
www.mydomain.com/NiceUrl1.html
www.mydomain.com/blog
www.mydomain.com/AnotherNiceUrl.html
RewriteEngine On
RewriteRule ^blog\.html$ /blog [L,R=301]
RewriteRule ^blog$ blog.html
That should work. The second line makes sure that any blog.html access gets a 301 - Moved Permanently HTTP header, and therefore opens /blog automatically. The third line makes /blog point to /blog.html on the server.

.htaccess file using 2 different rewrite rules

I've got a .htaccess file that has got a rewrite rule in it as follows which works fine:
RewriteRule ^solicitorsin([^/]+)/all/([0-9]+)$ /search/searchresults.php?county=$1&page=$2 [L]
What I'm looking to do is to keep using this for if the page variable is 2 or higher, but if it's 1 I want to 301 redirect to a separate url (the same site) say http://www.domain.com/solicitorsinCOUNTY/
The problem is that if I try doing this using a 301 redirect or a rewrite rule it still performs the above rewrite rule as well so I end up with http://www.domain.com/solicitorsinCOUNTY/?county=COUNTY&page=1
I haven't done much with .htaccess before so I'm not even sure if this is possible, can anyone help please? It would be much appreciated.
If you are using a rewrite rule, then put the rule for page=1 above the other rule and make sure you have the [L] flag.
Alternatively, you can use RewriteCond to prevent the rule from being run on specific URLS like this:
RewriteCond %{REQUEST_URI} !^solicitorsin([^/]+)/all/1$
RewriteRule ^solicitorsin([^/]+)/all/([0-9]+)$ /search/searchresults.php?county=$1&page=$2 [L]

Resources