How to redirect with .htaccess? - .htaccess

I have the following htaccess code already.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?page=$1
I would like that the htaccess redirect to
oszoczki.atwebpages.com/blog
when I type only
oszoczki.atwebpages.com

RewriteEngine On
RewriteRule ^$ /blog [R=301,L]
The ^$ in RewriteRule matches an empty URI and redirects to /blog.
R=301 represents a permanent direction.
L represents the last rule to be applied to this and ignore all others below it.
Update:
You can remove the L flag since redirection would have happened anyway as mentioned by #arkascha in the comments.

Related

Exact match specific word with RewriteRule in .htaccess

I'm setting up my .htaccess files with some RewriteRules, but unfortunatly I got stuck. I've found a lot of topics on this, but none mentioning my specific issue.
I have this:
RewriteEngine On
RewriteBase /
RewriteRule ^en/contact /contact.php?lang=en&slug=ok [L,NC]
RewriteRule ^contact /en/contact [L,R=301]
Purpose is to force the lang as a first parameter in the url if not set.
So: www.example.com/contact should be redirected (301) to www.example.com/en/contact which then again loads the content from /contact.php?lang=en&slug=ok
The problem I have is that I end up in a loop as /contact.php?lang=en&slug=ok gets loaded and is parsed by RewriteRule ^contact /en/contact [L,R=301] again.
How can I adapt this rule so it is only triggered for /contact and not for /contact.php or /en/contact or /contactme or any other url that contains contact
Thanks!
Your 301 redirect rule is causing the infinite loop as it matches the destination path of your first rule. What is happening is that when you request /en/contact your first rule rewrites it to /contact.php?lang=en&slug=ok and then ,your second rule matches the same uri and redirects it to /en/contact .
To fix this, You can either use END flag in your RewriteRule or use THE_REQUEST condition .
RewriteEngine On
RewriteBase /
RewriteRule ^en/contact /contact.php?lang=en&slug=ok [L,NC,END]
RewriteRule ^contact /en/contact [L,R=301]
Note : END flag works only on apache versions above 2.4 if your server version is less then 2.4 you could use a % {THE_REQUEST} condition to terminate the redirect loop
RewriteEngine On
RewriteBase /
RewriteRule ^en/contact /contact.php?lang=en&slug=ok [L,NC]
RewriteCond %{THE_REQUEST} /contact
RewriteRule ^contact /en/contact [L,R=301]
Make sure to clear your browser cache before testing these redirects.

Why is my Redirect 301 not working?

I already have a couple of rules set up such as
RewriteCond %{HTTP_HOST} ^www.pipcanadaltd\.com$ [NC]
RewriteRule .?$ http://ca.pipglobal.com%{REQUEST_URI} [R=301,L]
And I also have rewrites for a few PHP pages such as:
RewriteRule ^products/eye-protection-experts/$ prod-expert-eyewear.php [NC,L]
For some reason, when I went to create a simpler 301 redirect, it is not working. Here is what I have:
RewriteRule ^products/construction-channel-experts/$ ^products/construction-safety-solutions/ [R=301]
I'm really confused on why this doesn't work.
You can use this rule for 301 redirect:
RewriteRule ^products/construction-channel-experts/?$ /products/construction-safety-solutions/ [R=301,L,NC]
Note that ^ is used for matching start position in regex and it can only be used in pattern which is on left hand side.
You should keep redirect rules before internal rewrite rules.

.htaccess help, removing page1.html (pagination)

I am trying to remove abc.com/page1.html from the website as abc.com corresponds to same content as of abc.com/page1.html.
I have tried
RewriteEngine On
RewriteRule (.*)/page1.html /$1/ [R=301,L]
but it doesn't seems to work. Any suggestion?
RewriteRule already works on a URL-path of URL. Your full URL is abc.com/page1.html, but your URL-path is /page1.html. Leading slash is also excluded so in fact only 'page1.html' is matched against rules.
I believe this code will do a trick:
RewriteEngine On
RewriteRule (.*)page1.html$ /$1 [R=301,L]

.htaccess mod_rewrite 301 redirect with a pattern

I've changed my URL structure to remove the category from the URLs of item pages.
I need some way of redirecting all the old pages to the new pages, using mod_rewrite. I have already a quite elaborate and messy .htaccess file.
What I'd need is to redirect:
http://example.com/category-name/item-name/ to this http://example.com/download-item-name/
Right now, I've got this in the htaccess file.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301]
RewriteRule ^download-([^/]*)/screenshots/$ /screenshots.php?screenshots=$1 [L]
RewriteRule ^download-([^/]*)/screenshots/([^/]*)/$ /screenshots.php?screenshots=$1&shot=$2
#and now without trailing slash, so request will hit script which can do redirect.
RewriteRule ^download-([^/]*)/screenshots/([^/]*)$ /screenshots.php?screenshots=$1&shot=$2 [L]
RewriteRule ^feed/([^/]*)/$ /feed/?category=$1 [L]
RewriteRule ^download-([^/]*)-software/$ /category.php?category=$1 [L]
#this needs to stay above the below rule
RewriteRule ^download-([^/]*)/$ /software.php?shortname=$1 [QSA]
RewriteRule ^download-([^/]*)/([^/]*)/$ /software.php?shortname=$1&lang=$2 [QSA]
#and now without trailing slash, so request will hit script which can do redirect.
RewriteRule ^download-([^/]*)$ /software.php?shortname=$1 [QSA]
RewriteRule ^download-([^/]*)/([^/]*)$ /software.php?shortname=$1&lang=$2
RewriteRule ^downloading/([^/]*)/$ /download.php?downloading=$1
RewriteRule ^downloading/([^/]*)/([^/]*)/$ /download.php?downloading=$1&lang=$2 [L]
RewriteRule ^download/([^/]*)/([^/]*)/$ /send.php?key=$1&s=$2 [QSA]
RewriteRule ^FQ-([^/]*)/$ /content.php?page=$1 [L]
RewriteCond %{REQUEST_URI} ^(/[^/]+)(/.*)?$
RewriteCond %{DOCUMENT_ROOT}/categories%1/ -d
RewriteRule . /categories%{REQUEST_URI} [QSA]
Options -Indexes
FileETag None
ErrorDocument 404 http://example.com/404/
ErrorDocument 403 http://example.com/
#Some redirects
Redirect 301 /audio-video-players-codecs/zoom-player-max/ http://example.com/download-zoom-player-max/
Redirect 301 /audio-video-players-codecs/zoom-player-premium/ http://example.com/download-zoom-player-premium/
Redirect 301 /audio-video-players-codecs/zoom-player-pro/ http://example.com/download-zoom-player-pro/
Your .htaccess is messy because of simple things:
Rule #1: avoid mixing Redirect and RewriteRules directives. This will only confuse things. There's always a possibility to replace Redirect by RewriteRules directives.
Rule #2: if you don't need RewriteBase / (which is often the case, because the base is often /), don't write it. This will only confuse things. The shorter the better (that's what she tells me (just kiddin')).
Rule #3:: ^(.*)$ is the same as (.*). Here too, this makes things clearer.
Rule #4:: special rule for you: you can reduce by 2 almost all your RewriteRules using a thing like ^downloading/([^/]*)(/([^/]*))?/ which makes the second argument empty but match both one or two arguments (just valid in your Php that you can get an empty second argument). The shorter the better.
Then to answer your question: to redirect: http://example.com/category-name/item-name/ to this http://example.com/download-item-name/
RewriteRule ^category-([a-zA-Z]+)/item-([a-zA-Z]+)/ ^download-item-$2/ [QSA,R]
This should work
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

HTACCESS: *.subdomain.domain.tld redir to subdomain.domain.tld/*

sorry for the vague question title.
But anyways,
I have here a subdomain which i wish to pass on wildcard sub-subdomains and make a proper htaccess redirect to a sub-folder (relative to the server root) equivalent to the wildcard value such that
*.subdomain.domain.tld will redirect to subdomain.domain.tld/*
where * = wildcard value
I hope you get my question. Can someone shed some light on this? I would appreciate it very much =)
RewriteRule ^(.*).subdomain.domain.tld$ http://subdomain.domain.tld/$1 [R]
That should do the trick.
The [R] tells the server that this is a redirect.
You can also use optional codes, say
[R=301] # Moved permanently
[R=302] # Moved temporarily
[R=403] # Forbidden
[R=404] # Not Found
[R=410] # Gone
You have to read the subdomain's name with RewriteCond and then you can do the Redirect with RewriteRule, while the subdomain is saved in %1.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.subdomain\.domain\.tld$ [NC]
RewriteRule .* http://subdomain.domain.tld/%1 [L,R=301]
If you want to redirect *.subdomain.domain.tld/file.html to subdomain.domain.tld/*/file.html, you can replace the RewriteRule with this:
RewriteRule ^(.*)$ http://subdomain.domain.tld/%1/$1 [L,R=301]

Resources