URL Rewrite to remove variable entirely - linux

I need to fix a bunch of URLs from NetSuite to move into Magento
http://www.example.com/RANDOM-PRODUCT-NAME?sc=99&category=6666666
NetSuite makes these URLs from within the category (that is what ?sc=99&category=6666666 is for)
I would like to remove the variable completely from the url.
Any suggestions?

So, once again, I get no where with stackoverflow.
On the bright side, I did get the problem solved.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^sc=\d{1,2}&category=\d{1,7}$
RewriteRule ^([^.]+)$ http://www.somedomain.com/$1? [R=301,L]
that removes the query string from my URLs, it looks for the 'sc=' and a minimum of 1 or maximum of 2 digits and '&category=' and a minimum of 1 or a maximum of 7 digits.
Credit belongs to 'penders' #webmasterworld.com.
Thanks again.

Related

htaccess redirect if the url does not contain a specific character

I'm moving the site to a subdomain and need certain tag strings to go to the subdomain and some to remain on the main site. Problem is both have a similar tag system.
I need this type of request
https://www.site.co.uk/tags/example-tag
to go here:
https://sub.site.co.uk/tags/example-tag
but this type of request
https://www.site.co.uk/tags/view?tags=14-some-varriable
to remain unchanged and parsed to content without redirecting.
What would be the most recommended and best solution?
I have written some code to work around other redirects but this one is causing me a headache.
Cheers
For your this mentioned example, below should work.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.+)/(.+)
RewriteRule ^ https://sub.site.co.uk/%1/%2 [R]

mod_rewrite Replace underscores with plus sign and pass data to new URL

I recently switched to Wordpress and I dont want to lose some of the old pages.
The old website had pages using the following URL format
domain.name/gallery/word1_word2_word3_wordX.html
I need these links to be converted to:
domain.name/?s=word1+word2+word3+wordX&post_type=product
Basically I need to get everything after gallery/, remove .html, replace the underscores with plus sign and pass this to the new URL format between domain.name/?s= and &post_type=product
Any help is appreciated.
Thank you.
I played with this a bit and came up with a solution that works:
RewriteEngine On
# Note: This line is important! Without it, your URLs will look like example.com/home/you/public_html/gallery/etc, which is bad.
RewriteBase /
# Replace any _ with + and redirect
RewriteRule ^gallery\/(.*)_(.*) gallery/$1+$2 [R=301]
# Replace gallery/whatever.html with /?s=whatever&post_type=product
RewriteRule ^gallery/(.*)\.html /?s=$1&post_type=product [R=301]
This turns
http://example.com/gallery/foo_bar_baz_bam.html
into
http://example.com/?s=foo+bar+baz+bam&post_type=product
I drew on this post and this one to come up with this; you may find those helpful, as well.
Note: This does a redirect for each stage of the rewrite (one per _ and one for the final transformation). In theory, it's possible to do only one redirect by using [N] instead of [R=301] in the first RewriteRule (the .htaccess equivalent of a while loop), but I couldn't get this working without creating an infinite loop.

Forward slashes in part numbers are breaking mod_rewrite rule

I've a simple mod_rewrite rule
RewriteRule ^product/([^/.]+)/([^/.]+)/?$ product.php?partNumber=$1&partName=$2 [L]
It works great for 99.99% of products but there are 3 or 4 products which have a forward slash in their part number (eg PART001/1)
which rewrites to something like:
/product/PART001/1/part-name-here-for-nice-seo
Obviously this doesn't work as it's looking in an extra directory. I need the part number passed correctly as it's used to look up the index in the database and fetch all the product's information.
Is there any way round this?
Looks like you just need to change the ([^/.]+) grouping in your regex to also match slashes. Try:
RewriteRule ^product/(.+)/([^/.]+)/?$ product.php?partNumber=$1&partName=$2 [L]

.htaccess and dynamically generated SEO friendly URLs

I'm trying to build a website that may be called from the URL bar with any one of the following examples:
domainname.com/en
domainname.com/zh-cn/
domainname.com/fr/page1
domainname.com/ru/dir1/page2
domainname.com/jp/dir1/page2/
domainname.com/es-mx/dir1/dir2/page3.html
These page requests need to hit my .htaccess template and ultimately be converted into this php call:
/index.php?lng=???&tpl=???
I've been trying to make RewriteCond and RewriteRule code that will safely deal with the dynamic nature of the URLs I'm trying to take in but totally defeated. I've read close to 50 different websites and been working on this for almost a week now but I have no idea what I'm doing. I don't even know if I should be using a RewriteCond. Here is my last attempt at making a RewriteRule myself:
RewriteRule ^(([a-z]{2})(-[a-z]{2})?)([a-z0-9-\./]*) /index.php?lng=$1&tpl=$4 [QSA,L,NC]
Thanks for any help,
Vince
What's causing your loop is that your regex pattern matching /index.php. Why? Let's take a look:
First, the prefix is stripped because these are rules in an htaccess file, so the URI after the first rewrite is: index.php (query string is separate)
The beginning of your regex: ^(([a-z]{2})(-[a-z]{2})?), matches in in the URI
The next bit of your regex: ([a-z0-9-\./]*) matches dex.php. Thus the rule matches and gets applied again, and will continue to get applied until you've reached the internal recursion limit.
Your URL structure:
domainname.com/en
domainname.com/zh-cn/
domainname.com/fr/page1
domainname.com/ru/dir1/page2
domainname.com/jp/dir1/page2/
domainname.com/es-mx/dir1/dir2/page3.html
Either has a / after the country code or nothing at all, so you need to account for that:
# here -------------------v
^(([a-z]{2})(-[a-z]{2})?)(/([a-z0-9-\./]*))?$
# and an ending match here ------------^
You shouldn't need to change anything else:
RewriteRule ^(([a-z]{2})(-[a-z]{2})?)(/([a-z0-9-\./]*))?$ /index.php?lng=$1&tpl=$4 [QSA,L,NC]

Time-Dependent Rewriting and single digit TIME_MON/TIME_DAY variables

I am trying to redirect access to one part of my site before a certain date. I am using ISAPI_Rewrite 3 on IIS 6 but I believe ISAPI_Rewrite is compatible with mod_rewrite syntax. Imagine I wanted to redirect pages to a certain address before the 1st April 2012. I have found examples where people use something like this:
RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY} <20120401
The problem I have is that the server variables for TIME_MON and TIME_DAY appear to be returning single digits (e.g. 1 rather than 01).
So for the 1st May %{TIME_YEAR}%{TIME_MON}%{TIME_DAY} returns 201251 rather than 20120501.
201251 is less than 20120401 BUT the first of May is AFTER the first of April.
How do I get around this problem?
Looking at Time based rewrites - how to handle two seperate years? I have realised that I may be coming at this from the wrong direction.
I think something like the following could work for me:
RewriteCond %{REQUEST_URI} ^/Someplace/(.*)\.aspx$
RewriteCond %{TIME_YEAR} ^2012
RewriteCond %{TIME_MON} <4
RewriteRule ^(.*)$ /Someplace/systemdown.html [R=301,L]

Resources