htaccess match after ~ and pass as url parameter - regex

i want rewrite my url with htaccess. So, i want for example that
https://sample.com/~X6y2
has a match and it passes all behind the '~' as parameter to https://sample.com/req.php?id=[here]
is it possible with the '~'-character? so, if not, i need an alternative. Maybe '#'-Charakter, like tiktok.
So, it should match e.g.
https://sample.com/~AZaz09,
https://sample.com/~0123
but not
https://sample.com/0123,
https://sample.com/AZaz09,
https://sample.com/XY/~Z1234,...
I've tried several regex but nothing has provided the desired function :(
Best of these is (but not working, idk why):
RewriteEngine on
RewriteRule ^.*\/[\~]([a-z][^.]*)$ req.php?id=$1 [NC,L]
I'm new to regex and htaccess, so it's a bit difficult for me. I would be grateful if someone would provide a working htaccess including regex. Thx ✌🏼️

You want this solution
Check this solution
Demo: https://regex101.com/r/GJPZym/1

After a lot of trying, I've found a solution that is workin just fine.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*[\~]([A-Za-z0-9_-]*)$ req.php?id=$1 [L,NC]
where ~ can be replaced with anything u want :)

Related

RewriteRule - URL from Ugly to Pretty with variables not parsing correctly

I'd like some help rewriting an ugly url (http://example.com/image.php?id=1&name=Dog) to a pretty url (http://example.com/1/Dog) using Regex.
There are a lot of questions about the reverse version of this, and some other versions of the one I need. Nonetheless, they do not work.
RewriteRule ^image.php\?id=(.*)&name=(.*)$ http://example.com/$1/$2? [R=301,L]
This is the Regex I came up with. Testing it on Regex101 seems to be fine (?) https://regex101.com/r/uUlccx/2
It should be the reverse of this one:
RewriteRule ^([^/]*)/([^/]*)$ /image.php?id=$1&name=$2 [L]
But it seems like it isn't. Anyone know how the RewriteRule should be?
Edit: http://htaccess.mwl.be?share=47b41b23-1752-5a26-8432-196c95d1d669 htaccess tester
Edit 2: I got a little bit further, but it still doesn't seem to be working completely. The second variable is not taken in to the rewrite:
RewriteCond %{QUERY_STRING} ^(.*&)?id=(.*)&name=(.*)$
RewriteRule ^image\.php$ http://example.com/%1/%2/? [R=301,L]
RewriteCond %{QUERY_STRING} ^id=(.*)&name=(.*)$
RewriteRule ^image\.php$ http://example.com/%1/%2/? [R=301,L]
This seems to be the solution. Query values cannot be found in the normal rewrite rule?

URL Regex for Apache Mod Rewrite

I have been trying to work the regex out for this for a while now and I am struggling. Was hoping someone could help.
I have a website using apache mod_rewrite converting directories into get variables to 1 level. I am wanting to change this or add a seperate rule for the following
example.com/portfolio/plugins/jquery-tester
becoming
example.com/portfolio/handler.php?area=plugins&item=jquery-tester
I am trying to currently build up on php live regex but coming up trumps.
Try this:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/portfolio/(.+)/(.+)$ /portfolio/handler.php?area=$1&item=$2 [L]
Having the handler in the same directory as what you're matching can cause potential problems though (that's why the RewriteCond is there to check so you don't end up with an infinite loop).
If you have other rewrite rules, you may need to check that there aren't any conflicts.
RewriteRule ^/portfolio/[a-zA-Z\-_]+/[a-zA-Z\-_]+$ /portfolio/handler.php?area=$1&item=$2 [L]

Regex in .htaccess combining two negative conditions

could someone check this regex I wrote, it does what I wanted to achieve, but I'm not sure if it's the correct way to do it and if it's not slowing everything up
That's what it should do:
IF the URL Path is longer than the domain only
AND IF it doesn't contain the strings "/de" or "/en" at the beginning
THEN 301 it to the domain only
That's what I wrote:
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$
RewriteCond %{HTTP_HOST} ^(?!.*(/de|/en))
RewriteRule .* http://example.com/ [L,R=301]
... is there a better way to achieve that?
Thanks!!
Urs
I am assuming that you did a "mindfart" and the second cond should read %{REQUEST_URI} otherwise it makes no sense as HTTP_HOST will never include a /
In runtime it makes very little difference, but it is easier to understand if written as
RewriteCond %{REQUEST_URI} !(/de|/en)

Rewrite URL, regex help

I am using the following Rewrite URL:
RewriteRule /([^/?.]+) /somedir/somefile.aspx\?Name=$1 [NC,L]
which works great for my use, but I need to restrict it to only act on text that does not contain a filename... for example, if I use the url www.somedomain.com/SomeName it works fine, but it also fires if I use www.somedomain.com/TestPage.aspx
So I am not sure if I need an additional Rewtire rule, or if the current one can be modified to disallow any text with an extension, for example.
Any help with this regular expression would be greatly appreciated.
try adding this before the rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
this condition check if the file requested exists and returns true if it doesn't (as it's negated with a !).
if you need not to fire the rule also for the directories, then add also this line:
RewriteCond %{REQUEST_FILENAME} !-d

.htaccess rewrite rule preventing infinite loop

I have a directory named dollars that contains a file index.php. I would like for the url http://localhost/dollars/foo to translate to dollars/index.php?dollars=foo. This is the .htaccess file that I currently have in the dollars directorty:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^index\.php
RewriteRule ^(.+)$ index.php?dollars=$1 [L]
The idea being that any request other than a request to index.php should use the RewriteRule.
However, this does not work.
I've been looking for a while trying to figure out how to create the redirect I want, but I don't even know if I'm on the right track. Regex were never my thing. Thanks for any help!
A often-used solution for rewrites is to just check that the path being requested doesn't point to an actual file/directory, and rewrite it if it doesn't - since the rewritten URL will then point to an actual file, no loop occurs.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Amber's answer should get things working for you, but I wanted to address what was going wrong in your specific case. You had the right idea, but %{REQUEST_FILENAME} actually ends up being a fully qualified path here, so your regular expression should check for index.php at the end, not the beginning.
Consequently, you should find that this will work more like you expect:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !index\.php$
RewriteRule ^(.+)$ index.php?dollars=$1
Swapping out the RewriteConds for those that Amber mentioned would be less problematic if you added other things to that directory, though, so I'd recommend using that in place of this anyway.