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.
Related
I want to check if a parameter is present in a url in nginx and then rewrite. How can i do that?
The color is dynamic in the URLs
For e.g
If url is http://website.com/lunch-box/xxxxxabc then redirect user to http://website.com/lunch-box/.
If URL is http://website.com/lunch-box/xxxxxabc/ABCD123 no need to redirect. Need to load as it is.
I want to redirect if URL is matched. and xxxxxabc is dynamic text.
nginx version: nginx/1.16.1
# rewrite direct children of /lunch-box but not grandchildren+
rewrite ^(/lunch-box/)[^/]+/?$ $1 last;
Walking through the regex ^(/lunch-box/)[^/]+/?$
^ matches the start of a string (rewrite rules match against the path, not the full URI)
(/lunch-box/) matches the literal text /lunch-box/ and saves it for $1
[^/]+ matches one or more characters that are not a forward slash
/? matches zero or one forward slash
$ matches the end of the string
This strips off the path past what we've saved as $1, but only when that path is a direct child.
I have an ecommerce store with the product URL format:
/categoryname/subcat1name/subcat2name/1450--my-widget
I will shorten it to:
/1450--my-widget
I can do the change within the ecommerce software, but I need to set up a mod rewrite redirect for the old URLs.
To avoid matching URLs for categories, content pages, etc, as well as product URLs of the new format, I need to match on all these conditions:
Does not contain the string "/info/"
Contains a slash, followed by 1 or more characters, followed by another slash, followed by 1 or more digits, followed by "--", followed by 1 or more characters
What directive would work?
EDIT:
More examples of matching and non matching strings
Matches for old product url:
/a-category/this-category/333--my-widget
/some-cat/34--widgetname
Non matches:
/1918--widgetcategory/
/info/12--about-us
/quick-order
/login?back=my-account
/2050--my-widgetname
You can use this 301 redirect rule as your very first rule in site root .htaccess:
RedirectMatch 301 ^(?!.*/info/).*/[^/]+/([0-9]+--[^/]+)/?$ /$1
I want to make a catch all URL in my Django project...
It works fine, but now i can't go to my /admin and /blog page .. any idea how I can do this?..
My urls.py
url(r'^admin/', include(admin.site.urls)),
(r'^blog/$', 'apps.blog.views.index'),
(r'^([a-zA-Z0-9\-\_]*)', 'apps.review.views.show_search'),
You don't need to handle this manually with a RedirectView. You likely need to add the CommonMiddleware to your MIDDLEWARE_CLASSES settings if it is not already in there.
(inserting 'django.middleware.common.CommonMiddleware' into that list or tuple). It usually goes near the top of that declaration. Its documentation is here:
https://docs.djangoproject.com/en/1.5/ref/middleware/#django.middleware.common.CommonMiddleware
You also need to be sure APPEND_SLASH is set to True in your settings.
Furthermore, your last pattern is likely to match anything, which I suspect could be the real culprit of your problem. A URL without a trailing slash has to fail to match any patterns in order for it to automatically redirect to one with a trailing slash per that middleware. I think you can fix the last URL pattern by appending /$ to that pattern
The admin and blog urls have trailing slashes i.e. /admin/ and /blog/. The urls with the trailing slashes should work at the moment.
If you want to make the urls work without the slash, you could add entries for /admin and /blog, and use RedirectView to redirect.
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: ^[^/]+/?[^/]+/?$
I have 7 urls:
url: /v/3b89441db7986135e5eb9e1debf0cc23
url: /v/3b89441db7986135e5eb9e1debf0cc23/login
url: /v/3b89441db7986135e5eb9e1debf0cc23/logout
url: /v/3b89441db7986135e5eb9e1debf0cc23/access
url: /v/3b89441db7986135e5eb9e1debf0cc23/delete-attendee/:id
url: /v/3b89441db7986135e5eb9e1debf0cc23/edit-attendee/:id
url: /v/3b89441db7986135e5eb9e1debf0cc23/finalise
How could I write a rewrite rule that if these URLS are not matched, I redirect the user to another domain?
For example the part after /v/ is always a 32 character MD5 string. the :id part is always a number.
If you could give me a regex (regex has alas never been my forte) example for
/v/3b89441db7986135e5eb9e1debf0cc23
and
/v/3b89441db7986135e5eb9e1debf0cc23/edit-attendee/:id
that would be excellent.
This regex matches your urls:
/v/[a-f0-9]{32}(/[a-z-]+(/\d+)?)?
In english...
/v/ is a literal
[a-f0-9]{32} means 32 hex digits
/[a-z-]+(/\d+)? means "/" then at least 1 of (any lowercase letter or a dash) then "/" then some digits
surrounding a regex in (...)? means either one or none of them
FYI, this regex matches all urls given in question reasonably tightly
If you want to not match, use this:
^(?!/v/[a-f0-9]{32}(/[a-z-]+(/\d+)?)?$)
How about this:
\/v\/[a-f0-9]{32}(\/(login|logout|access|delete-attendee\/:\d+|edit-attendee\/:\d+|finalise)?
This will only match your accepted urls. You should adjust for your flavor or regex and appropriate escape chars.
You said that you want to redirect if URLs do not match, so first you must make a rule that matches all valid URLs, then prepend ! to negate the match.
RewriteEngine On
RewriteRule !^v/[0-9a-f]{32}(/(login|logout|access|delete-attendee/\d+|edit-attendee/\d+|finalise))?$ http://your-other-domain/ [R]
The above rule should be placed in the .htaccess file present in the root directory of your website.