I have posts like so:
http://example.com/blog/post-title
I want to convert this to
http://example.com/post-title
Using htaccess
I tried:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com/blog/([^\s\?]+)\s [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]
In this situation, you can indicate two capture groups (using parentheses) and only return the second of the two groups.
Example:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(blog)/(.+) http://example.com/$2
</IfModule>
The rewrite rule works as follows:
If...
the url starts with blog
followed by /
followed by at least one or more characters
then rewrite the URL so that the domain is followed by a / and then the one or more characters that were captured in the second capture group.
N.B. You won't need an [R=301] flag since providing a full redirect (ie. a redirect starting with the http:// or https:// protocol) implicitly indicates that this will be a 301 Redirect.
Related
I'm stuck with an 301 redirect in htaccess. I'd like to redirect from
https://subdomain.example.com/SOME_STRING
to
https://www.example.com/folder/SOME_STRING
The string can be different on any request. Can someone help me out here?
If these two hostnames point to the same place then you'll need to use mod_rewrite in order to check the hostname being requested.
Try the following at the top of your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.(example\.com) [NC]
RewriteRule ^ https://www.%1/folder%{REQUEST_URI} [R=301,L]
The %1 backreference matches the example.com in the preceding CondPattern - this simply saves repetition. The REQUEST_URI server variable contains the full URL-path from the request, including the slash prefix.
In fact, this is almost identical to another recent question that matches any subdomain:
how to redirect all subdomains to another with parameter after the trailing slash using htaccess?
To hardcode the hostname in the substitution string (as mentioned in comments) then you would do the following:
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule ^ https://www.example.com/folder%{REQUEST_URI} [R=301,L]
I'm trying to create clean URL with .htacces rewrite. I have two types of urls:
site.com/page.php?page=something
site.com/something.php
I need them both to be just site.com/something, with redirect from ugly to pretty url. So now I have the following rules, which don't work together, and I totally stuck with the redirect.
Options -Multiviews
RewriteEngine On
RewriteBase /
# page.php?page=about to about
RewriteRule ^([^/.]+)?$ /page.php?page=$1 [NC,L]
# something.php to something
RewriteRule ^([^/.]+)?$ $1.php [NC,L]
Will appreciate any help. Thanks in advance.
There are a few things that are incorrect in your example:
You cannot inspect the query string in a RewriteRule, only in a RewriteCond
Your RewriteRule lines are backwards - the first part is a regular expression match of the URL and the second is what you want it to be.
You will need to have an [R] rule as part of the rewrite to perform a redirect, otherwise it will just "rewrite" the URL the server sees and not change the actual URL.
Here is an example of your first rewrite, redirecting /page.php?page=foo to /foo. You first need a RewriteCond to inspect the %{QUERY_STRING} variable to see if it has page=... in it. We can use the character match ([^&]*) to grab all of the characters that are not an ampersand and store in a matching group. Next we perform a RewriteRule for page.php (note that we don't need the leading / because of the RewriteBase and that the . is escaped). If there is a match here, you want to redirect to the matching group from the RewriteCond - it is referred to with a %1 rather than a $1 like it would if it were from the RewriteRule. You will also want to append a ? to the end of your redirect which tells Apache to drop the query string so you don't end up with /foo?page=foo. Finally you will need [R=301] to perform a redirect with an HTTP status code of 301. The [L] indicates that that this is the Last rule you want to process if there is a match.
RewriteEngine On
RewriteBase /
# page.php?page=about to about
RewriteCond %{QUERY_STRING} page=([^&]*) [NC]
RewriteRule page\.php /%1? [R=301,L]
Your second rewrite is closer, but as in the first the logic is backwards. You want the first part to match *.php and then the second to indicate the redirect to /$1. Again you will need the [R-301] for the redirect.
# something.php to something
RewriteRule (.*)\.php$ $1 [R=301,L]
You can test this out on http://htaccess.madewithlove.be/.
Using http://example.com/page.php?page=foo, redirects to http://example.com/foo
1 RewriteEngine On
2 RewriteBase /
3 # page.php?page=about to about
4 RewriteCond %{QUERY_STRING} page=([^&]*) [NC]
This condition was met
5 RewriteRule page\.php /%1? [R=301,L]
This rule was met, the new url is http://example.com/foo
Test are stopped, because of the R in your RewriteRule options.
A redirect will be made with status code 301
Using http://example.com/foo.php redirect to http://example.com/foo
1 RewriteEngine On
2 RewriteBase /
3 # page.php?page=about to about
4 RewriteCond %{QUERY_STRING} page=([^&]*) [NC]
This condition was not met
5 RewriteRule page\.php /%1? [R=301,L]
This rule was not met because one of the conditions was not met
6 # something.php to something
7 RewriteRule (.*)\.php$ /$1 [R=301,L]
This rule was met, the new url is http://example.com/foo
Test are stopped, because of the R in your RewriteRule options.
A redirect will be made with status code 301
My site was hacked not too long ago and a lot of URLs were created in the following format:
http://example.com/prematurely.asp?skin=pspfsffdproblems=nq....
is there anyway I can re-direct all these wildcard URLs based on prematurely.asp to a single page? for example:
http://example.com/newpage
.htaccess file is as follows:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^nowcosmetic\.co.uk$ [NC]
RewriteRule ^(.*)$ http://nowcosmetic.co.uk/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/questions-and-answers\.html$
RewriteRule .* http://nowcosmetic.co.uk/botox.html [R=301,L]
RewriteRule ^prematurely\.asp /botox? [R=301,L]'
You can do this by adding a rewrite rule
RewriteEngine on
RewriteRule ^prematurely/(.*) /newpage [R=301,L]
As mentioned in comments, if these URLs are the result of a hacked site which has now been resolved then you are better off (SEO wise) serving a 404 (or 410 Gone) instead of redirecting.
However, if you still want to redirect...
re-direct all these wildcard URLs based on prematurely.asp to a single page
At the top of your .htaccess file (in your document root):
RewriteEngine on
RewriteRule ^prematurely\.asp /newpage? [R=301,L]
Note that RewriteEngine On should only appear once in your file (at the top).
This simply matches against "prematurely.asp" (ignoring the query string - as per your request). The trailing ? on the RewriteRule substitution strips the original query string from the rewritten URL.
I have two different kinds of rules in my .htaccess file. The first group matches on exact files, and then I have a generic catch-all for everything else. I have read you can use the [L] for rewrite rules, but is there an equivalent for Redirect 301? For example, my .htaccess file looks like this:
Redirect 301 /exact_page.html http: //www.newsite.com/new_page1.html
Redirect 301 /some_other_page.html http ://www.newsite.com/new_page2.html
RewriteEngine on
RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]
What I would like is for pages exact_page.html and some_other_page.html to be redirected exactly as shown, and everything else gets maps from the domain to the new domain, with the rest of the url intact. Instead, it looks to me like the first two Redirect 301's are being ignored, or more precisely, are being superseded by the final rule. Is there a way to tell apache to stop after it finds the first match?
I think something like this should work:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(exact_page\.html|some_other_page\.html)$
RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]
RewriteRule ^exact_page\.html$ http://www.newsite.com/new_page1.html [R=301,L]
RewriteRule ^some_other_page\.html$ http://www.newsite.com/new_page2.html [R=301,L]
I'm trying to setup a redirect rule for a new ExpressionEngine site.
I'm using the following code placed in my site's .htaccess file (the latter section is to remove index.php from ExpressionEngine's URLs - this works):
<IfModule mod_rewrite.c>
RewriteEngine On
RedirectMatch 301 ^/2010/example/*$ http://sub.domain.com/new-page/$1
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
A redirect of sort happens, but the URL I get is this:
http://sub.domain.com/new-page2010/example/
I've tried different combinations and it's driving me up the wall!! Any tips on where I'm going wrong?
The syntax for Apache's RedirectMatch Directive expects the use of parenthesis in the RegEx:
The supplied regular expression is matched against the URL-path, and
if it matches, the server will substitute any parenthesized matches
into the given string.
Using a RedirectMatch is equivalent to Redirect, but makes use of standard regular expressions, instead of simple prefix matching.
Therefore correcting your example, a valid RedirectMatch Directive would be:
RedirectMatch 301 ^/2010/example/(.*)$ http://sub.domain.com/new-page/$1
You can tap in the full power of Apache's rewrite engine by using a RewriteRule instead:
RewriteRule ^2011/example/(.*)$ http://sub.domain.com/new-page/$1 [R=301,L]
Depending on what you're wanting to redirect — a single page vs. entire sub-directory — you may need to modify the original URL path in each example.