How to implement .htaccess redirection with variables - .htaccess

i need to redirect with variables and i have no idea how to start, it would be great if you can support me, because i'm new to this.
from: */wp-content/uploads/[VAR]to: http://www.test.com/main/wp-content/uploads/[VAR]
*/ = everything in front of /wp-content
[VAR] = variable
thank you in advance for your help.

Use the below regex for matching:
\*
And replace it with:
http://www.test.com/main
I've no idea of how to do this with .htaccess, but I think you may know about this ;)

Related

Apache Mod rewrite: remove everything after ::

I moved a shop from xtc:modified to shopware.
The URLs look almost the same:
http://example.com/category/article::123.html
http://example.com/category/article
The only difference is ::123.html, 123 being different numbers.
Could you please provide me with the correct rewrite code to get rid of the :: and everything behind it?
I can't find the solution...
Thanks!
OK, so I did figure it out after a couple of more hours of trial and error:
RedirectMatch permanent (.+?)::.+$ https://www.example.com$1

Matching two parts of a string in htaccess

I'm having trouble with a regex to sort the following…
http://www.example.com/directory/listings/nostell-priory-2/
http://www.example.com/directory/listings/somerton-court-2/
http://www.example.com/directory/listings/shervage-court-2/
to look like the following…
http://www.example.com/directory/listings/nostell-priory/
http://www.example.com/directory/listings/somerton-court/
http://www.example.com/directory/listings/shervage-court/
I simply want to trim the -2 bit off the url but ONLY if the url contains the word listings.
Can anyone help?
Thanks,
James
Something like
RewriteRule ^directory/listings/(.*)-2/$ http://www.example.com/directory/listings/$1/ [R]

Issue with .htaccess rewrite when missing second variable

I've written this short piece of php code that requires 2 variables name and id, now the code itself works as intended and is not my problem, the problem is that I want to shorten the link to this file from 'http://www.mypage.org/folder/index.php?name=name&id=0' to 'http://www.mypage.org/folder/name;0', like so:
RewriteRule ^([a-zA-Z0-9]+);(.*)$ index.php?name=$1&id=$2
But if someone enters a link like 'http://www.mypage.org/folder/name' with out the ';' separator they get a 404 page.
Is there a way to write a sort of if statement that also checks for links with out the ';'?
The php page can handle a missing id by defaulting to '0' as well as a missing name.
Thanks in advance!
Make ;0 part or URL optional:
RewriteRule ^([a-zA-Z0-9]+)(?:;(.*))?$ index.php?name=$1&id=$2
or like this (if the above does not work in Apache)
RewriteRule ^([a-zA-Z0-9]+)(;(.*))?$ index.php?name=$1&id=$3

rewrite url with .htaccess

I need to rewrite a url like test.php?type=$1&val=$2
type will always be a string where as val could be a number or a string. I came up with the following
test/([a-zA-Z]+)/([a-zA-Z0-9|]+)/([0-9]+).html but as it was expected it will not work with something lke test/hello-world/23.html How can i included dashes in the expression?
You can escape them, so if you had
test/([a-zA-Z]+)/([a-zA-Z0-9\-|]+)/([0-9]+).html
This seems more like a question about regular expressions to me. test/([a-zA-Z-]+)/([a-zA-Z0-9|-]+)/([0-9]+).html should do. It's important to put the dash at the end of the character class.

'+' symbol in htaccess

I am using htaccess. I try to pass a value in url like 'C++'.
like "http://domain.com/Details/c++/detail.html"
I am retrieving the value in htaccess like
RewriteRule ^Details/([a-zA-Z_0-9_.,'&/-]+)/(([a-zA-Z_0-9_.,'&/-]+).html)$ index.php?page=$2&id=$1
But it returns only 'c'. Symbol '+' not accepted. I need the the value 'c++'.
Is there any solution?
Try Url encoding the + character.
"http://domain.com/Details/c%25%25/detail.html"
Is it possible to escape it, like http://domain.com/Details/c%25%25/detail.html. (I'm only guessing)
http://domain.com/Details/c%2B%2B/detail.html
A small consolation is: you are not alone. Since this problem is by design in URIs, eben big sites like Google have their problems with it:
http://www.google.com/search?q=c++
This does a search for just “c”.
Try it with the B flag:
RewriteRule ^Details/([a-zA-Z0-9_.,'&/-]+)/([a-zA-Z0-9_.,'&/-]+\.html)$ index.php?page=$2&id=$1 [B]

Resources