Rewrite URL, regex help - regex

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

Related

htaccess match after ~ and pass as url parameter

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 :)

Rewrite rule with pagination .htaccess

I have a URL like this:
http://example.com/category/title which comes from the link http://example.com/cview.php?url=title
I want to create pagination and to be like http://example.com/category/title/page/1 or
http://example.com/category/title/1
this comes from http://example.com/cview.php?url=title&pageno=1.
I have tried this in .htaccess without success
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^/]*)$/([^/]+)/?$ /cview.php?url=$2&pageno=$1 [L]
Can anyone help please?
RewriteRule ^category/([^/]*)$/([^/]+)/?$ /cview.php?url=$2&pageno=$1 [L]
You have an erroneous $ (end-of-string anchor) in the middle of the RewriteRule pattern. You also appear to have the backreferences $1 and $2 the wrong way round. You are also allowing an optional trailing slash, yet your example URLs do not use this. (An optional trailing slash potentially creates a duplicate content issue.)
If you allow both /category/title/page/1 and /category/title/1 then you are potentially creating a duplicate content issue. Presumably you are only linking to one of these URL formats?
Since the page number is a "number" then it makes sense to just match numbers, rather than anything - this also helps to avoid conflicts with other directives.
It doesn't look like you need the conditions (RewriteCond directives) that check the request does not map to a file or directory, since I wouldn't expect a request of the form /category/title/page/1 to map to a file or directory anyway?
Try the following instead (without the RewriteCond directives):
RewriteRule ^category/([^/]+)(?:/page)?/(\d+)$ /cview.php?url=$1&pageno=$2 [L]
This matches both /category/title/page/<num> and /category/title/<num>. The optional subpattern (?:/page) is non-capturing, so that it doesn't mess up the numbering of the backreferences.
Bear in mind also that the order of the rules in .htaccess is important in order to avoid conflicts.

Regex for htaccess to match pattern unless directory exists

I'm using regex in a htaccess file to catch /[username] and re-direct this to a username page, for example, mywebsite.com/john will forward to mywebsite.com/username?u=john. This works, but I now want to exclude anything that matches a directory which exists. So, if someone goes to mywebsite.com/sign-up, this is EXCLUDED for the re-direction, because that directory (/sign-up) exists. Is that possible, and is it normal practice? Thank you.
Edit: Ideally I don't want to have to exclude every existing directory manually. I'd like it to be automatic for all existing directories.
Use a rewrite condition before it. !-f means not an existing file, !-d means not an existing directory.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)$ username?u=$1 [L]

Problems with mod_rewrite and GET

So, I had a question earlier about mod_rewrite which you find here mod_rewrite changing /subpage/ to /subpage
But now I have a whole new problem with this... How do I make GET work?
Like if I need to pass some variables with GET, ex: mypage.com/subpage/?name=Jamie
My mod_rewrite looks like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)/?$ ?p=$1 [L]
I understand that the problem probably has to do with that I already pass and rewrite one GET request. So how do i tweak this to accept any other GET-request i might need to process?
UPDATE:
To clarify the problem a bit. If I try the url mypage.com/subpage/?name=Jamie page loads as if i used url mypage.com/subpage/ and "name" is never passed. Using my original url mypage.com?p=subpage&name=Jamie works as it should.
I guess i need to tweak this somehow... but how?
Thank you for taking your time reading my probably easy question!
Use the QSA flag — Query String Append:
RewriteRule ^([A-Za-z0-9-]+)/?$ ?p=$1 [L,QSA]
'qsappend|QSA' (query string append)
This flag forces the rewrite engine to append a query string part of the substitution string to the existing string, instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule.
try
RewriteRule ^([A-Za-z0-9-]+)/\?$ ?p=$1 [L]
as '?' is a meta character in regex, it needs to be escaped

.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.