Remove a part of the URL using REGEX and .htacess - regex

I want to replace this URL:
mydomain.com/posts/1659-artigos/etc-to
By this one:
mydomain.com/etc-to
Using .htaccess I'm trying the following:
RewriteEngine on
RewriteRule ^posts/1659-artigos/(.*)$ $1
But it isn't working. No redirect happens.
Can anyone help?
Thanks!

Converting my comment to answer so that solution is easy to find for future visitors.
You can use this code to get redirect working:
RewriteEngine on
RewriteRule ^posts/1659-artigos/(.*)$ /$1 [L,R=301,NC]
You need to use / before $1 for external redirect and make sure to use R flag for full redirect.

Related

htaccess remove ? from the url and redirect

I've some dynamic urls. If I took a url with a query parameter, it leads to 404 page. So I would like to do a redirection using htaccess. I tried many possible solutions, and none of them worked.
Url structure will be /jobs/job-***.html?something and which I need to redirect to /jobs/job-***.html
I tried something like this, but returned 500 error;
RewriteRule ^jobs/job-([0-9]+).html?$ jobs/job-$1 [NC, L]
Please help to solve this problem.
You may use this rule:
RewriteCond %{QUERY_STRING} .
RewriteRule ^jobs/job-(\d+\.html?)$ %{REQUEST_URI}? [NC,L,R=301]
? after $1 in target will remove any query string.
You may use the following regex:
^\/jobs\/job-(\d+)\.html\?\S*$
(This essentially captures the job number)
and then replace it with:
/jobs-$1.html
Demo

Rewrite rule regexp

I would like to ask you guys because I have a problem with one my rewrite rules and I can't figure it out how to write the good one.
I have this rule:
RewriteRule ^wp-content/uploads/(.*[^(.js|.swf)])$ authenticate.php?file=$1
What I would like to do is redirect the user to the authenticate.php every time when someone tries to open something in the wp-content uploads dir and I would like to send the filename to the php
For example:
http://domain.tld/wp-content/uploads/2015/11/something.pdf
redirect to authenticate.php?file=something.pdf
But unfortunately my regexp is broken. Could someone help me?
Really thanks for it!
Try with that in your .htaccess:
RewriteCond %{REQUEST_URI} !(?:js|swf)$ [NC]
RewriteRule ^wp-content/uploads/(.+)$ authenticate.php?file=$1 [NC,L]
For http://domain.tld/wp-content/uploads/2015/11/something.pdf the result: http://domain.tld/authenticate.php?file=2015/11/something.pdf
Try using this regex with negative lookaheads:
^.*?wp-content\/uploads\/.*?\.(?!js$|swf$)[^.]+$
The following URL will match the regex:
http://cpsma.ie/wp-content/uploads/2015/09/CPSMA-Newsletter-No-35-Sept-2015-2.pdf
However, a similar URL which ends in .js or .swf will not match. Hence, the following two URLs do not match the regex:
http://domain.tld/wp-content/uploads/2015/10/javascript.js
http://domain.tld/wp-content/uploads/2015/02/shockwavefile.swf
You can test this regex out here:
Regex101

.htaccess redirect is sending whole string, instead of partial

I partially have my .htaccess rule working. What I have currently is:
#tag to search redirect
RewriteCond %{REQUEST_URI} ^/tag\/*
RewriteRule ^(.*) https://www.testurl.co.uk/search-results?hsf=$1&id=12 [R=301,L]
What is currently happening, is where the $1 is, the entire of tag/* is going in there.
i.e request is tag/test URL generated is
https://www.testurl.co.uk/search-results?hsf=tag/test&id=12
when it should ideally be:
https://www.testurl.co.uk/search-results?hsf=test&id=12
Any help would be greatly appreciated.
Many thanks
You can use this rule:
RewriteRule ^tag/(.+)$ https://www.testurl.co.uk/search-results?hsf=$1&id=12 [R=301,L,QSA]
Pattern ^tag/(.+)$ will capture any value after /tag/ into group #1 and that is being used in $1.
Make sure to clear your browser cache before testing this.

Regular expression for referrer redirection

I need the regular expression to redirect a link like this:
www.mysite.com/forums/users/tom
To: http://www.mysite.com/dashboard/listings/tom
The problem is that the script I am using constructs links like this:
www.mysite.com/forums/users/tom/favorites/
And I need to redirect ONLY the www.mysite.com/forums/users/tom and NOT the www.mysite.com/forums/users/tom/favorites/ which I would like to leave as it is.
Is there any way to do this?
Please help me. :(
This should work and only redirect mysite.com/referrer/ and not mysite.com/referrer/topic:
RewriteEngine on
RewriteRule ^referrer/$ http://someurl.com [L,R=301]
EDIT:
RewriteEngine on
RewriteRule ^forums/users/(.*)/?$ http://www.mysite.com/dashboard/listings/$1 [L,R=301]

URL Rewriting in .htaccess - getting confused with params

I searched in the web to find a valid solution for my problem, but nothing works.
Hopefully you guys can help me.
I want to Rewrite a URL on an Apache doing like that:
(1.) www.example.com/en/rainbow.html => www.example.com/index.php?site=rainbow&lang=en
or
(2.) www.example.com/rainbow.html =>www.example.com/index.php?site=rainbow&lang=
or
(3.) www.example.com//rainbow.html =>www.example.com/index.php?site=rainbow&lang=
To be honest, my understanding for regex isn't that good.
Tried:
RewriteEngine On
RewriteRule ^(.+)[/](.+).html|(.+).html$ index.php/?lang=$1&site=$2 [QSA]
Result: No Error, but "site" has only a param at (1.).
RewriteEngine On
RewriteRule ^(.*[/]){0,1}(.*).html$ index.php/?lang=$1&site=$2 [QSA]
Result: Works fine, but i.e. lang="en/", should be lang="en"
In which way I can improve it and let it work correct?!
Thanks a lot!!
Try this:
RewriteEngine On
RewriteRule (/en/)?([^/]+)\.html$ index.php/?site=$2lang=$1 [QSA]
No need to escape forward slashes in mod-rewrite, by the way. But you do need to escape the ., which, as you know, stands in as a wildcard in regular expressions.
I'm not an expert in url rewriting, but i like koalas:
RewriteRule /+(([^/]+)/)?([^/]+)\.html$ /index.php/?site=$3&lang=$2 [QSA]