Redirect site to 410 except one URL - .htaccess

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

Related

301 redirect: redirect urls with .php to folder

In .htaccess I want to redirect urls like this:
/products.php/a7-frames
/products.php/a6-frames
to this:
/picture-frames/a7-frames
/picture-frames/a6-frames
So need to substitute products.php with picture-frames.
After a lot of Googling I tried this:
RewriteBase /
RedirectMatch 301 (.*)\.php/?$ https://www.domainname.com/picture-frames$1
But it doesnt work, if I enter this url: /products.php/a7-frames the browser says there are too many redirects and goes to:
/picture-frames/index
It's substituting the products.php for picture-frames which is great, but I'm not sure why it adds "index" on the end rather than the /a7-frames part of the url? How can I fix this?
You can use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+products\.php/([^\s?]+) [NC]
RewriteRule ^ /picture-frames/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^picture-frames/([^/]+)/?$ products.php/$1 [L,NC]
Make sure to clear your browser cache before testing this change.

301 redirect parameters without index.php in HTACCESS

After reading not too few threds in here and testing, I still haven't found a solution for some messy URL's I want to redirect.
Example 1:
I want to redirect the following URL:
www.mysite.co.il/binyanei/minisite/?ref=glob_AdvertisingArticle
to
www.mysite.co.il/
I found this syntax, but it's only relevant for URL's with index.php and it's just doesn't work for me:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=1
RewriteRule ^sub-dir/index\.php$ /path-to-new-location/? [L,R=301]
Redirect one clean URL to a new clean URL

How to Permanent Redirect a Directory That No Longer Exists (via .htaccess)

I am having trouble redirecting an entire directory that no longer exists on our server.
All variations of the following are not working and I simply get a 404 Page Not Found.
.htaccess file looks like this:
redirect 301 /non_existent_directory/ http://my.website.com/existent_directory/
Is it possible to use the Redirect 301 directive for this? Or is this only solvable by mod_rewrite?
Thanks
I even tried:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?my\.website\.com\/non_existent_directory\/$ [NC]
RewriteRule ^(.*)$ http://my.website.com/existent_directory/ [R=301,L]
No luck...
From the Redirect documentation, I would say
Redirect 301 /non_existent_directory http://my.website.com/existent_directory
or
Redirect 301 /non_existent_directory /existent_directory
should work, provided you are allowed to use this in a .htaccess file. See also Troubleshooting .htaccess files. You should test without 301 though, to prevent caching of bad redirects by the client.
If this doesn't work, you can try a RewriteRule of course
RewriteEngine On
RewriteRule ^/?non_existent_directory(.*)$ /existent_directory$1 [R,L]
But this is equivalent to the above Redirect directive.

Prevent htaccess 301 redirects on sub-domain

I have 301 redirects which direct /index.html to /site folder and when i created a sub-domain it also redirects the link to /site which causes 404 Not Found
For example: members.mysite.com redirects to members.mysite/site which causes 404 error can I add execption for specific forlder or something without changing the redirect.
.htaccess content
AddType text/x-server-parsed-html .htm .html
RedirectMatch 301 ^/index.html(.*)$ /site$1
You can use mod_rewrite to check for the host:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^your-main.domain.com$ [NC]
RewriteRule ^index.html(.*)$ /site$1 [R=301,L]
Take a look at the RewriteCond docs for a better description of its functionality. You can tailor the regular expression (^your-main.domain.com$) to fit more along the lines of your needs, like adding a (www\.)? in front to handle an optional "www".

How to remove a ? from a url with htaccess using 301 redirect

I need an htaccess 301 redirect for dynamic urls going from this url:
http://www.example.com/index.php/?content/page1
to
http://www.example.com/content/page1
My current htaccess rule is:
RewriteBase /
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^index.php(/.*)$ http://%{HTTP_HOST}$1 [R=301]
the problem is I get urls like this:
http://www.example.com/?content/page1
How can I remove that question mark (?) from the url. Also this is for about 20 different urls in this pattern. I would like the rule to work for all my urls needing to be 301 in this pattern.
End the target in a ?.
RewriteRule ^index.php(/.*)$ http://%{HTTP_HOST}$1? [R=301]

Resources