HTACCESS : RegEx match replace for URL - regex

I want the htaccess Redirect 301 to do the following using regex:
http://example.com/folder/abc_123_123.htm
to
http://example.com/abc-123-123.shtml
The 3 objectives of new URL are
delete /folder/
replace all _ with -
replace htm with shtml

Best I'm aware, your point #2 cannot be done without a RewriteMap, which requires some pesky configuration, or multiple rules.
This implementation should be generic and work for any number of underscores, but it's expensive in that it might trigger many redirect (in fact, potentially enough to fire redirect errors in browsers if you've tons of underscores in your paths):
RewriteBase /
RewriteRule ^(folder/[^_]*)_(.*\.html?)$ /$1-$2 [L,R=301]
RewriteRule ^folder/(*+)\.html?$ /$1.shtml [L,R=301]
Alternatives include enumerating use-cases as needed, or (better, I suspect) rewriting the request to a perl or php script and doing the regexp_replace + redirect 301 from there.
RewriteBase /
RewriteRule ^folder/(*+)\.html?$ /folder/redirect.php [L,QSA]

RewriteEngine On
RewriteBase /
RewriteRule ^folder/([a-z]+)_(\d+)_(\d+)\.htm$ http://example.com/$1-$2-$3.shtml [L,R=301]

Related

Is there a way to match /#user in Apache rewrite rules?

I am trying to redirect users from /#adam to /user/adam on my Litespeed server which uses .htaccess files.
I have already tried the following :
RewriteEngine On
RewriteBase /
RewriteRule ^\/#.+$ /user/$1
I expected it to redirect, but it does not. Even tried some variations like
RewriteEngine On
RewriteBase /
RewriteRule ^\/#(.+)$ /user/$1
You are close!
Do this:
RewriteBase /
RewriteRule ^#(.+)$ /user/$1
Notice I removed the leading \/
If you need to rigorously test htaccess rules then I highly recommend this gem: https://htaccess.madewithlove.be/

htaccess special character RewriteRule not working yet

We have a bunch of URLs that were indexed by Google with special apostrophes (url encoded as '%E2%80%99'). We corrected the urls on the server, but Google is still pointing there and we didn't want to interrupt any SEO mojo here. Any thoughts why this won't work?
Current rewrite rule in .htaccess file:
# remove apostrophes from a string
RewriteRule ^(.*)’(.*)$ /$1$2 [L,R=301]
RewriteRule ^(.*)%E2%80%99(.*)$ /$1$2 [L,R=301]
Example replace this URL:
http://example.com/santa%E2%80%99s-comin-to-town/
with this URL:
http://example.com/santas-comin-to-town/
Try using this:
RewriteRule ^(.*)’(.*)$ /$1$2 [B,L,R=301]
RewriteRule ^(.*)([^\w].+\d)(.*)$ /$1$3 [B,L,R=301]
using the % character can have adverse effects on rewrite rules:
(%..%..%..) or (\%..\%..\%..)
should also work, although make sure you provide the [B] flag on the end of the rule.
more info
Use this rule for using hex code in rewrite rules:
RewriteRule ^(.*)\xE2\x80\x99(.*)$ /$1$2 [L,R=301]

mod_rewrite: Redirect all urls ending with "/video/[anyfilename].mp4"

Sorry for this probably very noob-style question, but I just can't get it to work.
Here's my current .htacces file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^_res/.*$ - [L] # all URLs beginning with "_res" are not changed.
# put new fancy rule doing what I want here...
RewriteRule . index.php [L] # all other URLs are redirected to index.php
</IfModule>
This works fine. It's redirecting all urls except the ones starting with "_res/" to index.php.
What I need now is that all URLs matching "[anypath]/video/[anyfilename].mp4" will be rewritten to "content/[anypath]/video/[anyfilename].mp4" (which is the actual path of this file on the server - I can't use x-sendfile on this shared web space, thus I need to rewrite large file urls to their actual server locations to avoid php's fileread-function).
From my understanding of these RewriteRules, I think I have to place this rule just before the last one.
Unfortunately my regexp-expertise is practically non-existent.
What I thought should work is this:
RewriteRule ^(.*)/video/(^/*)\.mp4$ content/$1/video/$2.mp4 [L]
The regexp should mean "starts with any amount of any characters, followed by "/video/", followed by any amount of any characters which are not '/' and ends with ".mp4".
When I insert this into my .htaccess right before the last rule, urls ending with "/video/myvid.mp4" are still rewritten to index.php.
You see me clueless. Any suggestions?
Thanks alot in advance!
I think you want:
RewriteCond %{REQUEST_URI} !^/content/
RewriteRule ^(.*)/video/([^/]+)\.mp4$ content/$1/video/$2.mp4 [L]
And you want to add this rule above the one that routes to index.php

How to redirect from an old php script to a new php script using mod_rewrite

It's my first request here and hopefully I won't upset anyone.
Here's my story:
I have looked all over this place for a solution and wasn't able to find one. Here's hoping that someone can give some input.
I've basically managed to use Apache's mod_rewrite to create SEO-Friendly urls for my website.
E.g. Old path www.hostname/index1.php?session=user is now rewritten as www.hostname/user, which results into a SEO Friendly URL address.
However, the old path is still valid. I need to somehow redirect all the incoming old index1.php requests to the newer URLs, the SEO-Friendly ones, for the search engines to transfer the link popularities to the new ones. I believe I may have an infinite-loop redirect and that's why it's not working.
My code so far:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user [QSA,L]
The above rules never get hit when the htaccess file is parsed.
It hit me that I might be doing some sort of redirect loop here so I thought about renaming the index1.php file to index2.php and create something like:
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index2.php?session=user [QSA,L]
However, that failed too.
What would be the best approach to this? What am I doing wrong here?
Thank you!
Update your .htaccess rules to
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteCond %{QUERY_STRING} !no-redir [NC]
RewriteCond %{QUERY_STRING} session=user [NC]
RewriteRule ^/?index1\.php$ /user? [R=301,NC,L]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user&no-redir [QSA,NC,L]
You can't match against the query string (everything after the ?) in a rewrite rule, so you can't match against the session= part. You also can't simply match against the %{QUERY_STRING} var because that gets populated by you other rule when it rewrites the SEO friendly URL to the one with the query string. So you need to match against the actual request:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index1\.php\?session=([^&]+)&?([^\ ]*)
RewriteRule ^ /%2?%3 [L,R=301]

Can't get mod_rewrite to work when only having to rewrite certain urls

I have the following Rules setup in my .htaccess file
RewriteRule ^detail.php / [R=301,NC,L]
RewriteRule ^tempate.php / [R=301,NC,L]
What these do is perform a 301 redirect when detail.php and template.php is called, although this works this is not working when I enter mydomain.co.za/detail.php?product_id=2432&category=700 then it redirects the website to mydomain.co.za/?product_id=2432&category=700
I need any url where the filename is detail.php or template.php with any amount of parameters in the query string to redirect to the home page
I tried RewriteRule ^detail.php?(.*) / [R=301,NC,L] and this also not working. Any help or guide will be appreciated.
Replace your 2 RewriteRule lines with this line:
RewriteRule ^(?:detail|template)\.php$ /? [R=301,NC,L]
Note that question mark after /, that is a special mod_rewrite syntax to strip out any existing query string from original URI.
Also remember that RewriteRule only matches URI without query string therefore your attempt of ^detail.php?(.*) / [R=301,NC,L] won't work as you expected.