Conditional redirecting rules in .htaccess - apache

I need to apply the following rules in my .htaccess file-
Don't redirect if the url is mysite.com
Don't redirect if the url is mysite.com/index.php or mysite.com/anypage.anyextension
Don't redirect if the url has the word admin after domain as mysite.com/admin/...
Redirect if the url has the pattern mysite.com/anyword/ or mysite.com/anyword to mysite.com/somewhere/somepage.php?slug=anyword
.htaccess is a completely new world to me.
Could someone help?

If you are looking to have a specific folder perform a redirect, for example a news folder the template would be
RewriteEngine On
RewriteRule ^news/([A-Za-z0-9-]+)/?$ /news/index.php?slug=$1 [NC,L,QSA] # Process posted item
This redirects yousite.com/news/my-first-article to yoursite.com/news/index.php?slug=my-first-article
Just be sure to replace news and index.php with the pages actually handling the command

Related

Redirect with .htaccess to exact URL provided

So I want to redirect from old sites url to new one. Lets say example.com/en/some/stuff/foo/bar needs to be redirected to example.com/some/stuff.
Here is what I have at he moment:
Redirect 301 /en/some/stuff/foo/bar/ /some/stuff/
The problem is that I end up being redirected here example.com/some/stuff/foo/bar, but I need as I defined inside .htaccess example.com/some/stuff.
How to redirect properly to exact URL I have provided without anything extra.
You can use this redirect rule with regex in your site root .htaccess:
RewriteEngine On
RewriteRule ^en/(some/stuff)/.+ /$1 [L,NC,R=301]
Make sure to test it in a new browser to avoid old cache.

.htaccess Redirect all Subdomains to an URL

how can I redirect all of the subdomains to a URL
this is the scenario :
abc.xxx.com goes to abc.xxx.com/index.php?id=abc
www.xxx.com/abc or xxx.com/abc also goes to the same URL: abc.xxx.com/index.php?id=abc
and please bear in mind there is no /abc folder on the website
and the file index.php is the root file of the main domain
I want to keep the abc in the subdomain for SEO purpose.
Thanks in advance.
I think you must first check if URL does not contain "index.php?id=" to prevent infinite loop.
RewriteCond %{REQUEST_URI} !index\.php\?id= [NC]

.htaccess redirecting only pages with no subfolder

I am using .htaccess to redirect pages from an old website to a new site. The URL structure has changed completely, but thanks to a SEO person I have a complete list of URLs I have to redirect and take care of.
My .htaccess looks like the following:
Options +FollowSymlinks
RewriteEngine On
Redirect 301 /some-guy http://www.new.domain/kontakt/
Redirect 301 /some-other-guy http://www.new.domain/kontakt/
...
Redirect 301 /de/some-page http://www.new.domain/some/subpage/
Redirect 301 /de/another/page http://www.new.domain/nice-page/
Of course the URLs can be even longer and contain query strings and stuff like that. The new website has a different URL structure and does not need query strings and nothing.
Now, while the first two redirects work perfectly, every redirect which contains a subfolder fails to redirect to the target location. The last URL for example redirects to
http://www.new.domain/de/another/page
What am I missing here?
Try
Redirect 301 ^de/another/page http://www.new.domain/nice-page [L]
I got it working now with little modifications to Anup Saunds code (and therefore using mod_rewrite).
I used the following structure which seems to work fine with all links I have and sets the status code for search engines.
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^what/ever/your/path-is(.*)$ http://www.new.domain/oh-what/a-nice-path/ [R=301,nc]

How to redirect to subdomain but then allow normal use of site

So I have my site, www.domain.com.
For a week or so I want to direct all traffic going direct to the site to subdomain.domain.com, a little promo page about an upcoming feature. I want visitors to then be able to continue to the site as normal though after they've read it, so a continue to www.domain.com/index.php link.
How can I do that with in the htaccess file? Everything I've tried so far messes up when clicking the continue link.
Thanks
with .htaccess you could use a 302 temporary redirect, but it would be for a whole sub folder as far as I know.
Another way would be to redirect with JS/server site language to the subdomain, create a cookie, then redirect back to www.domain.com/index.php .
the 302 redirect is explained here: How do I redirect my site using a .htaccess file?
You would need to have a .htaccess for the root folder point to your subdomain
Note that this is only possible if you enable mod_proxy in the Apache config of domain.com otherwise URL will change after redirect.
Enable mod_proxy, mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^index\.php$ http://subdomain.domain.com/ [L,NC,P]

301 redirect not working

I made a subdomain, and I am trying to redirect a page from the original domain to the subdomain, with the same dir structure.
My original page is this:
http://www.comehike.com/outdoors/alaska_hiking.php
I put this rule into the .htaaccess file under comehike.com/outdoors/ directory:
RewriteRule ^outdoors/alaska_hiking.php http://hiking.comehike.com/outdoors/alaska_hiking.php [R,L]
But as you can see from visiting the original url, it doesn't redirect. Any idea why?
Thanks!
I use apache server.
That RewriteRule line works for me when I stick it in my .htaccess file, are you sure you have RewriteEngine On?