Htaccess Not Working - Redirect 301 - regex

Due to some bad URLs, we generated some links that don't work and I want to redirect them with a 301 redirect to clear up some webmaster tools issues with Google.
So, we have this URL like this:
http://www.site.com/subdomain/z//-products
*Note that subdomain is variable, the rest of the url is static.
As a side note, this URL makes no sense, that's why I want to redirect it. It should be something like this:
http://www.site.com/bedroom/z/12345/bedroom-furniture-products
Anyway, we had these bad URLs being dynamically generated. We've fixed them, but google picked them up and keeps trying to crawl them. I want to create an htaccess rule to 301 redirect them and the issue should wash out eventually.
Here's what I tried with htaccess to no avail:
^(.*)/n//-products/?$ $1 [R=301,B]
I've also tried all kinds of permutations of this and it's not working. I suspected it was an entity escaping issue, but my research led me to add the [B], but that didn't seem to work either. It's like the redirect rule is working, but it's just redirecting to the original page.
What am i missing here?

I believe anubhava is correct, in that there is inconsistency between the sample URL you describe /subdomain/z//-products and the RewriteRule you attempted to apply. Not sure if this is a typo or not. It may even be the case your copy/paste operation actually added the "/n" literally.
Anyhoo, let us presume that you want to make the rule work with /subdomain/z//-products:
RewriteRule ^/([^/]+)/z//\-products/?$ http://www.site.com/$1 [R=301]
See the example 1 slides of this PDF to get the quick first portion. It is much faster than using (.*).
We literally match the z character and the surrounding slashes. We escape the - character, then we do the rest of the URL and optionally match the trailing slash. We use, if memory serves correctly, an "external" style redirect so that the robots re-open a separate HTTP connection, appending the matched backreference, and hand off the status code.
Let me know if that works.

Enable 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
RewriteBase /
RewriteRule ^subdomain/z/-products/?$ /$1 [L,R=302,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Related

Regex for htaccess redirect (with exceptions!)

I am looking forward to redirect hundreds of URL belonging to the path https://example.com/fragen/, for example:
https://www.example.com/fragen/amazonerstattungen/
https://www.example.com/fragen/sonderbetriebsvermoegen/
https://www.example.com/fragen/troedel/
to a single URL like https://www.example.com/wiki/
Could you please suggest me the correct regex expression to be included in the .htaccess to attain this goal?
Besides, a few URLs belonging to the /fragen/ path should be redirected separately.
I suppose that these redirects should precede the regex expression in the code block in object. Is that correct?
Using mod_rewrite, near the top of your root .htaccess:
RewriteEngine On
# Specific Redirects
RewriteRule ^fragen/specific-url$ /somewhere-else [R=302,NC,L]
RewriteRule ^fragen/another-url$ /another-url [R=302,NC,L]
# Redirect everything else in "/fragen/" to "/wiki/"
RewriteRule ^fragen/ /wiki/ [R=302,L,NC]
The regex ^fragen/ matches any URL-path that starts fragen/. Note that the URL-path matched by the RewriteRule directive does not start with a slash.
And yes, as you suggest, specific redirects need to precede the generalised redirect.
Note that these are 302 (temporary) redirects. Always test first with 302s to avoid potential caching issues. Only change to a 301 (permanent) redirect - if that is the intention - once you have confirmed it works as intended.
Aside: This might not have the SEO benefit you are hoping. A many-to-one redirect like this is often seen as a soft-404 by search engines. And users often bounce. It is often preferable to implement a custom 404 response and offer an explanation to the user where the page has gone.

What does (|/)$ do with mod_rewrite?

I've seen examples in htaccess files using mod_rewrite where everything is done through one php file and different URLs are redirected back to index php.
RewriteRule ^registration(|/)$ /index.php
I'm curious as to what (|/)$ does/is. I've read a lot of stuff and can't seem to find any mention of the use of a vertical bar in mod_rewrite and if I remove this, the redirect still works fine.
The vertical bar stands for a logical OR, and lets you specify either a trailing slash after 'registration' or not.
I prefer using a '?' after the slash, making it optional:
RewriteRule ^registration/?$ /index.php

Redirect 301 - remove .html extension from URLs

I would like to remove the .html extension from my urls, located into specific directory and redirect 301 them.
Here is how the structure looks like:
mysite.com/category/nameofcategory/pagenumber.html
The thing is that nameofcategory and pagenumber could be any letter or number.
Could you please help me with this?
I wouldn't recommend having your content scattered in many html-files in different folders. This becomes very impractical if you for example want to change the layout of your pages.
Storing the content in a database is a much better solution. If that's not possible perhaps the html files could contain only the formatted text content and a back end script could embed that content to a layout when the page is requested.
This requires that the mod_rewrite module is enabled in the Apache configuration.
In both cases all of the requests would be routed through the back end script and the .htaccess might look something like this:
RewriteEngine on
RewriteRule ^category/([^/.]+)/([^/.]+)/?$ index.php?category=$1&page=$2 [L]
This part of the regex: ([^/.]+) matches and captures a string that doesn't contain the characters / or . and is 1 characters long or longer. The captured strings can be referenced with $1, $2 and so on.
Now the pretty urls like mysite.com/category/foo/bar work. In addition we need to define a rule that redirects the old urls ending in ".html". The rule required might look something like this:
RewriteRule ^category/([^/.]+)/([^/.]+).html$ category/$1/$2 [R=301,L]
One thing to remember while testing and adjusting the redirects is that the redirect may get cached in the browser which may lead to confusing results when testing.
To remove the .html extension on the URL and 301 redirect to the extensionless URL you can try the following in the .htaccess in your "specific directory":
RewriteEngine On
RewriteBase /specific-directory
RewriteRule ^(.*)\.html$ $1 [R=301,L]

How to redirect from specific subdirectory to a subdomain via .htaccess?

I've been trying to redirect this URL (and all its substructures):
http://example.com/archive/
to (and its corresponding substructures):
http://archive.example.com/
For example: http://example.com/archive/signature/logo.png ==> http://archive.example.com/signature/logo.png
I tried to generate an .htaccess rule using a generator and evaluating it by looking at the regex, which I can understand (I think).
The result was the following rule:
RewriteEngine On
RewriteRule http://example.com/archive/(.*) http://archive.example.com/$1 [R=301,L]
The way I see it, the server will proccess any URL that starts with http://example.com/archive/ , will capture the string that comes next and will change the whole initial portion with the subdomain structure and append the captured string.
Unfortunately, this doesn't seem to work neither on my server, nor on online testing tools such as: http://htaccess.madewithlove.be/
Is there anything I'm missing there?
Thank you!
You should be able to try it this way.
RewriteEngine On
RewriteRule ^archive/(.*)$ http://archive.example.com/$1 [R=301,L]
Note that I did not make it dynamic as you didn't specific if you will have more URL's that need to work this way as well or not.

How to remove the query part of the rewritten URL after it has been remotely redirected?

Either I am too tired to see what I am doing wrong or there is something important I am missing here.
Basically I have a simple set of rewrite rules which are used in conjunction with a central dispatcher file (index.php) to handle requests coming for HTML, CSS and JavaScript files separately and they look like this.
RewriteEngine on
RewriteRule (.+)\.html$ index.php?action=view&url=$1.html [L]
RewriteRule (.+)\.css$ index.php?action=resource&type=css&url=$1.css [L]
RewriteRule (.+)\.js$ index.php?action=resource&type=js&url=$1.js [L]
Long story cut short these rules work fine however I've been notified by the SEO agency responsible for the site that there is an error in one of the URLs which needs to be permanently redirected (301) to the correct link. Since its just one URL that requires redirecting I have chosen to use Redirect instead of URL rewriting and added the following rule.
Redirect 301 /page1.html /page2.html
This works well too except for the fact that after the remote redirection is done for page1.html I get the query part (?action=view&url=page2.html) displayed in browsers address bar. I perfectly understand that the HTMl rewriting rule simply added the query string part after it was done with the URL but what would I need to do to get rid of the query part after a remote 301 redirection is performed.
Just to add I tried the URL rewrite method too but it seems that whatever I do the L flag is simply ignored and the HTML rewrite rule is still executed.
RewriteRule ^page1\.html$ page2.html [L,R=301]
That's a rewrite redirect and should cut off the query string. Put it before your other 3 rules, otherwise it will be ignored.
I don't know how much the solution may change with the web-server and the web-server version, but what worked for me was "When you want to erase an existing query string, end the substitution string with just a question mark".
See "Modifying the Query String" at http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule (Apache v2.4)
So,
RewriteRule ^page1\.html$ page2.html? [L,R=3xx]
The R flag is needed for the new URI to be showed and not the original with the query string. But even without the R flag, the query string will not be passed.