Regex to Find URLs with only one slash - regex

I am testing a website and need to target the pages I want to include in the test with Regex.
I will be targeting only product pages which all have a single slash in the URL (The URLs do not show http:// in them).
Here are the URLs I need to match:
The ones I want look like this:
www.example.com/just-one-slash
The ones I don't want look like this:
www.example.com/more-than-one/slash
www.example.com

This should work for your case: ^[^/]+/?[^/]+$
For the answer to be generic, with a trailing slash it would just need /? at the end, like this: ^[^/]+/?[^/]+/?$

Related

How to redirect folder request to a subfolder with htaccess and GREP

I have about 200 folders that contain pdfs and docs. They should all be redirected to a subfolder in the same directory. There are also folders and files in the same directory that should not.
The folders all have the same name-structure (123-name-name with - or _ mixed) which match the following with GREP in BBEdit:
(\d{3}(_|-).+)
So I tried something like:
RedirectMatch 302 ^/([\d{3}(_|-).+)]/(.*)/?$ /subfolder/$2
placed in the same folder as the ones I want to redirect.
I have no idea how to set the match correctly.
A URL like example.com/images/123-name-name/somefile.pdf (or doc, docx) should be found at example.com/images/subfolder/123-name-name/somefile.pdf
But the code above results in a Internal Server Error.
It would be great if someone could help me in that.
You could use:
^(.*?\/\d{3}(?:-|_)\w+(?:-|_)\w+\/)(.*)$
to capture both parts of the url in capture groups, and replace with $1kunden/$2, so your line will look like:
RedirectMatch 302 ^(.*?\/\d{3}(?:-|_)\w+(?:-|_)\w+\/)(.*)$ $1kunden/$2
This would insert kunden/ in between the rest of the url and the file name.
If you want to specify that the filename must have an extension (ie, include at least 1 .) you could replace the regex with:
^(.*?\/\d{3}(?:-|_)\w+(?:-|_)\w+\/)(.*?\..*?)$
EDIT My bad, to prevent the recursion, you can use a negative look-ahead to ensure the subfolder doesn't already exist in the path:
RedirectMatch 302 ^((?!.*subfolder\/).*?\/)(\d{3}(?:-|_)\w+(?:-|_)\w+\/)(.*)$ $1subfolder/$2$3
This will prevent a url that already contains subfolder/ from redirecting again.

Django greedy characters missing in reverse urls

I am trying to use the following rule to map urls in django
url(r'^(?P<permalink>[a-zA-Z0-9_-]*)/?$', views.page, name='page'),
This should match pages like
site.com
site.com/super-awesome-page/
This works however the reverse urls provided by the url template tag are missing the trailing / i.e. "site.com/page" these do get captured pattern but I want my links to show up in my page correctly how can I get this to work correctly.
I would have expected since the trailing slash is greedy it should be included in the reverse url.
The trailing slash is optional in your regex, so django doesn't generate it for you for the reverse URL.
The easiest solution is probably to make the trailing slash non-optional. With the default settings, django will redirect the non-slash version to the slash-version. Easiest to just standardize on that.

Cleansing special characters from URL search queries

When I use the search form on my website, the URL ends up like this:
http://website.dev/catalogsearch/result/?q=WhatISearchedFor
I'd like to make it so that the URL is cleansed of special characters so that if I tried to go to
http://website.dev/catalogsearch/result/?q=$What#I&Searched\\For
I'd end up at the same URL above
Here is the current rewrite rule I have, but I'm new to regular expressions/htaccess so I'm not sure if it's formatted correctly.
RewriteRule ^catalogsearch/result/?q=(/[^a-z0-9\s\']/i)$
How would I go about doing this?

Redirect all URLs starting with STRING that does NOT include a SUBSTRING

I need some help with regex.
I'm building some 301 rules for an .htaccess
I need to redirect all urls starting with a specific string excluding one that has a given word in the match-all part
this is the simple rule I'm using:
/my/sample/url/(.*)
I need to edit the (.*) part to say: anything except if contains "foobar"
if contains "foobar" I need a different 301 rule
This looks like is working:
^(?!.*foobar)/my/sample/url/(.*)
does anybody have a better solution?

IIS7 URL Rewrite with Regex

I'm trying to do a URL rewrite when a user accesses a certain URL of my site:
Accessed URL: https://client1.domain.com
Rewritten URL: https://new-client1.otherdomain.com
My site has many URLs that point to it, so the simple HTTP redirect module will not be a valid solution for this. What would the Regex be and what would I want to fill in for each section in a rewrite rule?
Thanks
Try this:
s/client1.domain/new-client1.otherdomain/g
You can use this regex pattern to search for client1.domain in order to replace it:
(?<=//)([^.]+)\.domain
Replace it with a backreference to client1 and the new domain like so:
$1\.otherdomain