Redirect hundreds of urls with htaccess - .htaccess

hello my url structure right now for the majority of my links is:
www.url.com/category1/sample-keyword.html
I am looking to redirect them to the new url that has dropped the word sample from the url structure ie to this:
www.url.com/category1/keyword.html
what should i put in htaccess that auto redirects all the urls in the www.url.com/category1/ section to redirect to the new url structure?

This should do the trick:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*/)sample-(.*)$ $1$2 [L,R=301]
It will match all URLS with the substring /sample- and strip it from the URL. Depending on your site organization, you may need to adjust the pattern, but that should be a good jumping-off point.

RewriteEngine On
# Redirect sample-*.html to *.html
RewriteRule ^\/?category([0-9]+)\/sample\-([^\/]+)\.html$ http://www.url.com/category$1/$2.html [R=301]
# Serve *.html
RewriteRule ^\/?category([0-9]+)\/([^\/]+)\.html$ page.php?category_id=$1&keyword=$2 [L]

Related

URL Rewriting in. Htaccess

I have a website on a test server and I want to rewrite URL for this website because it is very long
I wish our visitors instead of entering this URL:
http://staging.company.fr/site2.it/s...oject2/public/
enter this URL:
www.monsite.com
I created a file. htaccess:
RewriteEngine On
RewriteRule ^/~(.+) http://www.monsite.com/~$1 [NC,L]
but does not work
While in .htaccess mod_rewrite doesn't match leading slash in a URL since it is applied per directory. Therefore following should work:
RewriteEngine On
RewriteRule ^(.+)$ http://www.monsite.com/$1 [L,R=301,NE]
This will redirect every URL in your existing domain other than home / to monsite.com
Reference: Apache mod_rewrite Introduction

Remove Last Character of URL with .htaccess

Somehow google managed to pickup some URLs on my site with a ) at the end which is causing a bunch of 404 links. The links look something like this:
"http://mysite.com/page.php?id=42523"
but what google has in its search results is:
"http://mysite.com/page.php?id=42523)"
Is there a way with .htaccess that I can determine if a url ends with ) and redirect it to the proper URL?
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)\)$
RewriteRule ^(.*)$ /$1?%1 [L,R=301]

htaccess permanent redirect index php to root

I'm using magento and all my urls contains index.php.
Can anyone give me the code to remove index.php
My url looks like this
example.com/index.php/hi-how-are-you.html
I want it redirect to
example.com/hi-how-are-you.html
I want if anyone accessed the page using index.php it should redirect to non index.php url
This has been discussed time and again in the Magento forums, see e.g. this thread.
To redirect visitors from /index.php/path to /path you can use the following mod_rewrite Rule:
RewriteRule ^index.php/(.*) $1 [R=301,QSA,L]
To exclude the admin area, add the following condition before (!) the RewriteRule
RewriteCond %{REQUEST_URI} !^/index.php/admin/
RewriteRule ^index.php/(.*) $1 [R=301,QSA,L]
This tells mod_rewrite not (!) to rewrite if the request begins (^) with /index.php/admin

.htaccess 301 redirect path and all child-paths

I want accesses to e.g. www.thisdomain.com/docs/path1/path2 to redirect to www.thatdomain.com/path1/path2
(Note that docs is not a part of the new path)
I have the following on www.thisdomain.com:
RewriteEngine on
RewriteRule ^docs/* http://www.domain.com/ [R=301,L]
If I access www.thisdomain.com/docs, it directs to www.thatdomain.com, but if I access a child-path like www.thisdomain.com/docs/path1/path2 it fails. Is it possible for the redirect to intercept the child-path access and redirect as I need? If so, any pointers?
Thanks.
With regular expressions, * means any number of the previous atom, which will match /docs and /docs/. Try this:
RewriteEngine on
RewriteRule ^docs$ http://www.domain.com/ [R=301,L,QSA]
RewriteRule ^docs/(.*) http://www.domain.com/$1 [R=301,L,QSA]
(QSA is query string append, so /docs/foo?bar=baz won't lose the ?bar=baz.)
According to section "Redirect Old domain to New domain using htaccess redirect" of the first Google result which I found searching for "htaccess redirect" (without the double quotes), this piece of code will suffice:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
According to their description of the technique, it will redirect the directory the .htaccess file is placed in recursively (including any child paths), just as you intend. Of course, mod_rewrite needs to be present for the rewrite directives to work.

How to redirect a single web page from one domain to another using .htaccess

How do I get the following redirect to work?
olddomain.com/employee-scheduling-software.html
To redirect to
newdomain.us/employee-scheduling-software.html
I do have mod_rewrite on, but I'm basically a complete novice in this area
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^employee-scheduling-software.html$ http://newdomain.example.org/employee-scheduling-software.html
</IfModule>
You can change the rule into:
RewriteRule ^employee-scheduling-software.html$ http://newdomain.example.org/employee-scheduling-software.html [R=301]
which will send a 301 Moved Permanently header to the browser, so it updates its bookmarks and stuff.
You could use this code in .htaccess on olddomain.com:
RewriteEngine on
RewriteRule ^employee-scheduling-software\.html$ http://newdomain.us/employee-scheduling-software.html [R,QSA]
Since the ^employee-scheduling-software\.html$ is a PERL regex, you need to escape the dot in ".html" with a backslash (\.).
This will just redirect employee-scheduling-software.html to the new domain. If you want to redirect all files to the new domain, use:
RewriteEngine on
RewriteRule ^(.*)$ http://newdomain.us/$1 [R,QSA]

Resources