Redirect 301 - remove .html extension from URLs - regex

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]

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.

htaccess Regex 301 Redirect

I'm having a hard time getting a permanent redirect to work. I would like this to happen, using regular expressions.
OLD URL: https://example.com/olddir/other_name_here/123456/garbage.jpg
NEW URL: https://example.com/newdir/other-name-here-123456/
Note the change from underscores to dashes and the fact that I'm throwing the extra bits away after the numeric string. I've tried this but it isn't working (page doesn't exist and still getting a 404):
RewriteRule ^/olddir/other_name_here/([0-9]{6})/.+ /newdir/other-name-here-$1/ [R=301,L]
I have a few hundred names in the "other_name_here" directory location, so if I could dynamically change underscores to hyphens that would be good but not necessary. olddir and newdir are actual names and can be hardcoded. What am I doing wrong? Thanks!
Try the following actions:
If not present in your .htaccess add the following 2 lines before your RewriteRule:
Options +FollowSymLinks
RewriteEngine On
Change your regex in the following way by adding a $:
RewriteRule ^/olddir/other_name_here/([0-9]{6})/.+$ /newdir/other-name-here-$1/ [R=301,L]
Try to access to your new URL pages directly and confirm that they are accessible.
Finally, I would recommend this website for details about the .htaccess detailed configuration: https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules

301 Redirect with Partial Filename Change

Here are some examples of the old URLs:
http://www.test.com/files/Old_Name.zip
http://www.test.com/files/Old_Name.php
http://www.test.com/files/Old_Name_Setup.php
http://www.test.com/files/Old_Name_100_Setup.php
Here are what the new URLs look like:
http://www.test.com/files/New_Name.zip
http://www.test.com/files/New_Name.php
http://www.test.com/files/New_Name_Setup.php
http://www.test.com/files/New_Name_100_Setup.php
I need to edit my .htaccess file to reflect these changes, so when people visit the old URLs, they are taken to the new URLs.
I've seen something like
Redirect 301 /files/Old_Name.php http://www.test.com/files/New_Name.php
Unfortunately, this way would require me to do this for every single file, and there are a lot. I'm guessing I'm going to have to use regex, but I haven't seen a solution online that only modifies part of the filename. I'm assuming this should be able to be done in 1 line as the structure change between old and new URLs is consistent.
You can use in /files/.htaccess:
RewriteEngine on
RewriteRule ^Old_Name(.*)$ New_Name$1 [NC,L,R=301]
Or in root .htaccess:
RewriteEngine on
RewriteRule ^files/Old_Name(.*)$ files/New_Name$1 [NC,L,R=301]

Htaccess Not Working - Redirect 301

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.

Mod-rewrites on apache: change all URLs

Right now I'm doing something like this:
RewriteRule ^/?logout(/)?$ logout.php
RewriteRule ^/?config(/)?$ config.php
I would much rather have one rules that would do the same thing for each url, so I don't have to keep adding them every time I add a new file.
Also, I like to match things like '/config/new' to 'config_new.php' if that is possible. I am guessing some regexp would let me accomplish this?
Try:
RewriteRule ^/?(\w+)/?$ $1.php
the $1 is the content of the first captured string in brackets. The brackets around the 2nd slash are not needed.
edit: For the other match, try this:
RewriteRule ^/?(\w+)/(\w+)/?$ $1_$2.php
I would do something like this:
RewriteRule ^/?(logout|config|foo)/?$ $1.php
RewriteRule ^/?(logout|config|foo)/(new|edit|delete)$ $1_$2.php
I prefer to explicitly list the url's I want to match, so that I don't have to worry about static content or adding new things later that don't need to be rewritten to php files.
The above is ok if all sub url's are valid for all root url's (book/new, movie/new, user/new), but not so good if you want to have different sub url's depending on root action (logout/new doesn't make much sense). You can handle that either with a more complex regex, or by routing everything to a single php file which will determine what files to include and display based on the url.
Mod rewrite can't do (potentially) boundless replaces like you want to do in the second part of your question. But check out the External Rewriting Engine at the bottom of the Apache URL Rewriting Guide:
External Rewriting Engine
Description:
A FAQ: How can we solve the FOO/BAR/QUUX/etc. problem? There seems no solution by the use of mod_rewrite...
Solution:
Use an external RewriteMap, i.e. a program which acts like a RewriteMap. It is run once on startup of Apache receives the requested URLs on STDIN and has to put the resulting (usually rewritten) URL on STDOUT (same order!).
RewriteEngine on
RewriteMap quux-map prg:/path/to/map.quux.pl
RewriteRule ^/~quux/(.*)$ /~quux/${quux-map:$1}
#!/path/to/perl
# disable buffered I/O which would lead
# to deadloops for the Apache server
$| = 1;
# read URLs one per line from stdin and
# generate substitution URL on stdout
while (<>) {
s|^foo/|bar/|;
print $_;
}
This is a demonstration-only example and just rewrites all URLs /~quux/foo/... to /~quux/bar/.... Actually you can program whatever you like. But notice that while such maps can be used also by an average user, only the system administrator can define it.