redirecting URL with .htaccess not working - regex

Trying to do a simple URL rewrite with .htaccess but it does not seem to be working.
Want to access http://www.example.com/shop/index.php?route=information/information&information_id=11
using the URL http://www.example.com/MyGreatClub2013
This is what I have in my .htaccess which is stored at the www.example.com root level
RewriteEngine On
RewriteRule ^/MyGreatClub2013$ /shop/index.php?route=information/information&information_id=11 [NC,L]
Am I doing something stupidly wrong?

Remove leading slash:
RewriteEngine On
RewriteRule ^MyGreatClub2013/?$ /shop/index.php?route=information/information&information_id=11 [NC,L,QSA]
mod_rewrite rules when used in .htaccess don't match leading forward slash as .htaccess is per directory.

Here's a decent guide: http://www.javascriptkit.com/howto/htaccess.shtml
Hope it helps.

Related

Redirect URL except subpages

I'm trying to redirect everything under www.mywebsite/asd/ except for three URLs and any subcontent, www.mywebsite/asd/A/, www.mywebsite/asd/B/ and www.mywebsite/asd/A/.
I tried the following pattern, but it seems to be faulty:
RedirectMatch 301 ^/asd/((?!A/|B/|C/).)* /asd/new-target/index.html
The regular expression does what I want, when I test it in a regex editor. But in htaccess it doesn't work. What do I have to change?
You can use this rule:
RedirectMatch 301 ^/asd/(?!A/|B/|C/|new-target/)(.*)$ /asd/new-target/index.html
And test it after clearing your browser cache. Make sure this is your first rule in site root .htaccess and there is no .htaccess in /asd/ directory.
Alternatively you can use this mod_rewrite rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/ads/(?:A|B|C|new-target)/ [NC]
RewriteRule ^/?asd(?:/.*)?$ /asd/new-target/index.html [L,NC,R=301]

URL rewriting to a folder if url has one pattern and ignore the rule if url has another pattern

I have following directory structure for the project
example
admin/
cdn/
client/
.htaccess
When the url is
http://192.168.1.121/example/ or
http://192.168.1.121/example or
http://192.168.1.121/example/category/productname or
http://192.168.1.121/example/content/12 and so on,
it should rewrite to client/ folder.
But if url has http://192.168.1.121/example/cdn, it should ignore the rule.
Can anybody help me on this. How to write the rule in .htaccess
Thanks
You can use this /example/.htaccess:
RewriteEngine On
RewriteBase /example/
RewriteRule ^(?!client/|cdn)(.*)$ client/$1 [L,NC]
(?!client/|cdn) is negative lookahead to ignore the rule when URI is /example/cdn OR /example/client
Something like that
RewriteRule ^/example/cdn . [L] Do nothing if it starts with "/example/cdn" and stop there
RewriteRule ^/example /client$0 [L] Prepend "/client" if it starts with "/example"

htaccess redirect a plus sign

I try to make a redirect of a URL from my old site to my new site, but it does not work.
I think that the plus symbol is giving me problems.
RewriteRule ^nl/pageid/junair+compressor+onderhoud(|/)$ /nl/persluchttechniek/page/nieuwsberichten/compressor-onderhoud-junair [R=301,L]
Try this redirect rule in your root .htaccess of old site:
RewriteRule ^nl/pageid/junair[\s+]compressor[\s+]onderhoud/?$ /nl/persluchttechniek/page/nieuwsberichten/compressor-onderhoud-junair [NC,B<R=301,L]
Make sure this is your very first rule below RewriteEngine line. Also check there is no other .htaccess in your system.
The + is presumably a URL encoded space, try replacing it in the pattern with a \s.
RewriteRule ^nl/pageid/junair\scompressor\sonderhoud(|/)$ /nl/persluchttechniek/page/nieuwsberichten/compressor-onderhoud-junair [R=301,L]

Redirect to different domain and URL structure

I need to permanently redirect part of a website to a different domain and URL structure.
Everything that matches:
example.com/year/month/day/page-name/
and
www.example.com/year/month/day/page-name/
Needs to redirect to:
www.sample.com/sub/sub2/year/month/page-name/
For example:
example.com/2013/11/09/a-great-page/
should permanently point to:
www.sample.com/sub/sub2/2013/11/a-great-page/
But I don't want to redirect any other pages on the site:
example.com/a-random-page
Shouldn't redirect anywhere.
My htaccess chops and regex skills are a little rusty, so any help would be greatly appreciated!
Enable mod_rewrite and .htaccess through httpd.conf (if not already enabled) and then put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{SERVER_PORT}s ^(443(s)|\d+s)$
RewriteRule ^([0-9]{4}/[0-9]{2}/[0-9]{2}/[^/]+/?)$ http%2://sample.com/sub/sub2/$1 [L,QSA,R=301]

Regex htaccess rewrite to strip url prefix

So many questions come close to it, but not quite there, and I'm afraid regex makes my head spin!
After a site recode, which slightly amended some urls - which we don't want to go 404 in Google, I am looking for a 301 redirect to remap:
http://www.oursite.com/listing-product-name-1234.html
to
http://www.oursite.com/product-name-1234.html
...you can see we just want to drop "listing-". The "1234" is a unique ID per product, and the product name can be multiple words (hyphen separated) by the way.
Thanks very much for the help regex heads!
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 ^listing-(.+?\.html)$ /$1 [L,R=301,NC]
Try this:
RewriteCond %{REQUEST_URI} ^/listing-(\w+-)+\d+\.html
RewriteRule ^listing-(.*) $1