erase part of a URL using htaccess rewrite - .htaccess

i need to remove part of Joomla/Virtuemart generated SEF URI using .htaccess
the URI represents a menu hierarchy and structured this way:
online-store
- inner-store
-product-catalog
this is the resulting URI:
www.domain.com/online-store/inner-store/product-catalog
i would like to change it to:
www.domain.com/online-store/product-catalog
thought this might help but its not making any difference
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^online-store/inner-store/\d+-(.+) /online-store/$1 [R=301,L]
i know its not considered good practice but i can't change the menu structure.
any suggestions ?

This regex \d+-(.+) will match 1 or more digits followed by hyphen followed 1 or more any thing
Try this code instead:
RewriteRule ^(online-store)/inner-store/(.*)$ /$1/$2 [R=301,L,NC]
Make sure this is first rule in your .htaccess and use a different browser to test it to avoid caching issues.

Related

Pattern Matching htaccess conflict

I have migrated my site to new software and am trying to ensure that older links are appropriately redirected to the new url's. I thought I had it working until I received the message from Google regarding increasing 404's.
I seem though to be causing a conflict between the new htaccess requirements and my changes to address the old links.
So the new links look like this:
http://www.exampledomain.com/search/?q=searchterm
And in the htaccess they are picked up like this:
RewriteRule ^search/(.*)$ search.php?q=$1
The above is working as it should.
The old links can look like either of these:
http://www.exampledomain.com/search/searchterm/
or
http://www.exampledomain.com/search/searchterm
I had put this in to the htaccess
RewriteRule ^search/(.*)/$ http://www.exampledomain.com/search.php?q=$1 [R=301,NC,L]
If I don't add the first rule then the new url's just bring up 404's.
If I add second rule the it stops the searchterm being passed and conflicts with the first rule.
Have tried a few things but think there must be an issue with the matching or something else i'm missing.
Any ideas appreciated.
** Added **
So after the first reply I made the change as suggested and it caused a couple of issues but switching the order of the rules has fixed that but has not quite fixed the issue
So now I have this:
RewriteRule ^search/(.*)/?$ http://www.exampledomain.com/search.php?q=$1 [R=301,NC,L]
RewriteRule ^search/(.*)$ search.php?q=$1
The above works for these url's now:
http://www.exampledomain.com/search/?q=searchterm
and
http://www.exampledomain.com/search/searchterm
But for url's like this:
http://www.exampledomain.com/search/searchterm/
it results in this with a trailing slash which prevents the search:
http://www.exampledomain.com/search.php?q=drama/
So just need to remove or not have the trailing slash
I think you are just missing a ? in the rule since the / is optional:
RewriteRule ^search/(.*)/?$ search.php?q=$1 [R=301,NC,L]
Update to address trailing slash:
I'm guessing that the .* is consuming the trailing / before the next rule. To fix that, we need to exclude it from the match:
RewriteRule ^search/([^/]*)/?$ search.php?q=$1 [R=301,NC,L]
Update to address added case where parameter appears after slash:
I'm not sure if you mean literally /search/q=searchstr or /search/?q=searchstr so I will attempt to address both cases.
If it's the latter, which is a true query string, place this rule above the first using QSA in order to pass the query string along to the new URL:
RewriteRule ^search/$ search.php [R=301,NC,QSA,L]
To address the first variation (without the ? query string), you will need to place this rule above the first, which literally looks for the q=:
RewriteRule ^search/q=(.*)$ search.php?q=$1 [R=301,NC,L]
And since I get the feeling you'll update this question again to ask about what happens if there is a trailing slash, I'll go ahead and modify that rule to handle this case as well:
RewriteRule ^search/q=([^/]*)/?$ search.php?q=$1 [R=301,NC,L]
If these rules still don't solve every case for you, then you're dealing with some really bad code from your previous URL and I feel very sorry for you. :P

Htaccess - Detecting the URL

For my family members I was giving each person their own subdomain
(sister1.mydomain.com, sister2.mydomain.com, etc...)
I was using PHP to detect the domain, and then I'd load information related to the subdomain dynamically.
I'd like to get rid of the subdomains and use the power of .htaccess
My goal is to give the same URL:
www.mydomain.com/sister1
www.mydomain.com/sister2
www.mydomain.com/mommy
www.mydomain.com/daddyo
Obviously, I don't plan to have literal working directories for each person.
I'd pass the "sister1" portion to a process.php script that takes care of the rest.
I've figure out how to do it by manually typing each RewriteRule in my htaccess file:
Options +FollowSymLinks
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteRule ^/?sister1$ process.php?entity=sister1 [L]
RewriteRule ^/?sister2$ process.php?entity=sister2[L]
RewriteRule ^/?mommy$ process.php?entity=mommy[L]
RewriteRule ^/?daddyo$ process.php?entity=daddyo[L]
I feel this is the long way of doing it.
Is there a more universal way of extracting the text after the first "/" forwardslash, and passing it to process.php?entity=$1 ?
I tried it this way:
RewriteRule ^/([A-Za-z0-9-]+)/?$ process.php?entity=$1 [NC,L]
I'm getting the apache 404 error: "Not Found".
It is because you have a mandatory / in the beginning of your rule, i.e., you are always looking for something like /sibling in the URL. Your first examples have that first forward slash as optional due to the question mark after it.
You do not need a beginning forward slash - normally the rewrite rule picks up stuff after the domain name
www.example.com/string/mod/rewrite/gets/is.here
So just remove the starting slash and it should work.

htaccess 301 redirect while replacing characters in URL - Helicon ASAPI ReWrite module

I have an old site that is being rebuilt. Instead of using a folders structure, it is using sub-domains. The segments are different, but the redirect itself is pretty simple. I can handle it like so:
RewriteRule ^segment/blog/view$ http://blogs.site.com/segment/article [R=301,NE,NC,L]
RewriteRule ^segment/blog$ http://blogs.site.com/segment [R=301,NE,NC,L]
So if I had www.site.com/segment/blog, it will now go to blogs.site.com/segment.
If I had www.site.com/segment/blog/view/catchy_name_goes_here, currently it redirects it to blogs.site.com/segment/article/catchy_name_goes_here and I NEED it to go here: blogs.site.com/segment/article/catchy-name-goes-here.
My issue comes from a decision to change the separator in the URI. The old articles were built with underscores '_' and the new articles are built with hyphens '-'.
How can I replace the underscores in the article titles with hyphens?
Try these rules:
RewriteRule ^/segment/blog$ http://blogs.site.com/segment [R,I,L]
# replace _ by - repeatedly
RewriteRule ^(/segment/blog/view)/([^_]*)_+(.*)$ /$1/$2-$3 [I,N,U]
# all _s gone, now do a redirect
RewriteRule ^/segment/blog/view/([^_]+)$ http://blogs.site.com/segment/article/$1 [R,I,L,U]
I ended up having to use the following. I don't know how many people this might effect due to the unique settings for this site in particular, but thought I would post the answer so it could help anyone that might need it.
The full settings on this are a server running IIS with Server 2k. The site consists of several static content pages, vb script, classic ASP, dot Net, and this is all intertwined with ExpressionEngine pages. It's a mess to say the least. To top it off, Helicon Tech's ASAPI Rewrite Module version 3 is running on the server for .htaccess usage. No sub-expressions, groupings, etc. were taking or being followed/processed. The index.php rule was getting bypassed as well.
This all said, I ended up with the following which parsed everything I needed.
RewriteRule ^index.php/segment/blog/view/([^_]*)_+(.*)$ http://www.site.com/index.php/segment/blog/view/$1-$2 [R,NC]
RewriteRule ^index.php/segment/blog/view/([^_]*)$ http://blogs.site.com/segment/article/$1 [R=301,I,L,U]
RewriteRule ^segment/blog$ http://blogs.site.com/segment [R=301,NE,NC,L]
RewriteRule ^/segment/blog$ http://blogs.site.com/segment [R,I,L]

Rewrite URL with three dynamic parameters

I am working on my website where URL is something like:
http://mywebsite.com/series.php?sid=1&part=1&name=Introduction%20to%20HTML
I have tried a rule in my htaccess which is:
RewriteRule ^([^/]+)/([^/]+)/([^\.]+)\.html$ /series.php?sid=$1&part=$2&name=$3
In order to rewrite URL in form of:
http://mywebsite.com/1/1/Introduction to html.html
Unfortunately, it is not working for me.
Please someone help me in sorting out the issue. I would also love it if someone makes me understand how it worked.
I'm newbie for .htaccess.
In above rule, I can see three different parts like:
1st part: ^([^/]+)
2nd Part: ([^/]+)
3rd part: ([^\.]+)\.html$
These three parts are separated by backslash. How they work and how can I utilize the same Rewrite URL form for more than 3 let say 4 or 5 parameters and less than 3 like 2 parameters?
Thank You!
Your rule is correct, just add L flag to end the rule and make sure .htaccess and mod_rewrite are enabled through Apache config.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^\.]+)\.html$ /series.php?sid=$1&part=$2&name=$3 [L,NC,QSA]

.htaccess dynamic to static URL

I'm trying to make my dynamic URL's into static looking URL's.
This is a typical URL that I now have:
http://www.somedomain.com/design/index.php?p=about
I would like it to be: http://www.somedomain.com/about
So far, I've gotten it to look like this: http://www.somedomain.com/design/about.html
This is the Rewriterule I'm using: RewriteRule ^([a-z]+).html$ index.php?p=$1 [L]
How would I modify it so it would look like this: http://www.somedomain.com/about?
Thanks for any/all help!!!
Very much appreciated!
Using rewrite rules to give 'static' URI is NEVER a good idea.
A few other ideas you can use:
Make the 'about' page a directory (folder) with a file called index.php or index.html in it. This way the URL shows http://example.com/about/ and the information you wish can still be displayed as needed.
Use the POST method instead of GET methods. This will display as http://example.com/about.php (Note: there is no ? or other parameters behind that.)
Utilize both methods to give a 'seamless' URI on page transitions.
Rick, you're on the right track. You need to read the Apache rewrite documentation. For your docroot/.htaccess start it with:
RewriteEngine On
RewriteBase /
Then generalised version of your rule:
Rewrite Rule ^(\w+)$ index.php?p=$1 [L]
This will rewrite any requests which are for a word string to index.php. You need to be aware that the rewrite engine rescans the .htaccess file if a match has occured so you need to make sure that you don't create a loop. In this case the replacement string has a "." in it and the pattern doesn't, so this won't occur, but for more complex cases you may need to 'guard' the rules with one or more RewriteCond statements. Again, read the Apache documentation.

Resources