I have a situation where I am trying to use Apache's RedirectMatch directive to redirect all users to a HTTPS URL except a single (Linux) user accessing there own webspace. I have found a piece of regular expression code that negates the username:
^((?!andy).)*$
but when I try using it in the directive:
RedirectMatch ^((?!andy).)*$ https://www.example.com/
the URL:
http://www.example.com/~andy/
still gets redirected to the HTTPS URL:
https://www.example.com/
When I want it to ignore the redirection.
I am not an expert in regular expressions (or Apache), so any help in getting this working would be greatly appreciated.
Thanks,
Stephen
Edit:
OK, the plot thickens... if I comment out the line:
# RedirectMatch ^((?!andy).)*$ https://www.example.com/
and restart Apache, and then try the URL:
http://www.example.com/andy
I get a 404 response, which is expected. But, if I try the URL:
http://www.example.com/~andy
The redirect is triggered and I am redirected to:
https://www.example.com/
and as I said, the redirect is commented out and I have restarted the server. How can this happen? So this is not just a regex thing, it seems Apache is redirecting without instruction!
Something along the lines of this maybe? (Uses mod_rewrite instead of mod_alias)
RewriteCond %{REQUEST_URI} !^/~andy(/|$) ...
RewriteCond %{SCRIPT_URI} ^http:
RewriteRule ^/(.*) https://mysite.com/$1 [R=307]
Related
I have a requirement where I need to 301 redirect an old site URL to a new one. So the format of the URLs will be:
www.oldsite.com/test/xxx.pdf
should be redirected to
www.newsite/test/xxx.pdf.
So let's say, if someone hits www.oldsite.com/test/123.pdf, it should be redirected to www.newsite/test/123.pdf.
And 123 can be any number/name, so it should be replaceable using a RegEx.
Any idea?
Yes. This should do it:
RewriteEngine on
RewriteRule ^(test/[^./]+\.pdf)$ http://www.newsite.com/$1 [L,R=301]
To go in your root .htaccess file on oldsite.com.
the actual url structure is:
domain.com/browse-string.html
examples of old url:
domain.com/browse-nature-videos-1-date.html
domain.com/browse-science-videos-1-views.html
I have changed the world browse so the new url is
domain.com/newword-string.html
How can I 301 redirect old to new?
Thank you in advance
You can do a simple RewriteRule and check for the start of the string to be "browse".
RewriteEngine On
RewriteRule ^browse(.*)$ /newword$1 [L,R=301]
Make sure you change newword to the new word you want.
You can test it here: http://htaccess.mwl.be/
Sorry for handwriting abilities
Place the following in the .htaccess file and it should work:
Redirect 301 /oldfile.html /newfile.html
Edit: Since you need rule/pattern based redirection, the following thread should fix you up:
HTACCESS redirection with a word replacement in url
This URL
http://www.example.com/news/2138-gideon-rothschild-listed-leading-lawyer-expert-guides
needs to be redirected to this URL
http://www.example.com/news/gideon-rothschild-listed-leading-lawyer-expert-guides.
Here is another example:
http://www.example.com/articles/2140-can-you-trust-your-trust
to
http://www.example.com/articles/can-you-trust-your-trust
The client wants the page id removed from the URL. Can this redirect be done in the htaccess file or do I need to do it using PHP?
Yes, it can be done using a RedirectMatch rule.
Place this rule in your site root .htaccess:
RedirectMatch 301 ^/(articles)/\d+-(.+)$ /$1/$2
The very basic rule could look like the following:
RewriteRule ^([0-9-]+)(.*)$ /$2 [L,R=301]
You would need to fine-tune it a bit if you want only news/ or articles/
This is such a stupid question, I'm so close to solving it but regex is killing me – I'm using the JavaScript history api on various pages of a test.
Server side, I would like to redirect:
/test/1
/test/2
/test/3
All to
/test
So I am using
Redirect 301 /test/1 /test
Redirect 301 /test/2 /test
Redirect 301 /test/3 /test
This all works, but I would like to redirect
Redirect 301 /test/1000 /test
So, here is where I am going wrong, after some hacking, googling etc I'm using:
Redirect 301 /test/([0-9]+) /test
to replace all other lines of code, but it's totally failing. What's the line to match all /test/{number} so I can redirect them all at once?
Redirect doesn't support regex, but RedirectMatch does: httpd://apache.org/docs/current/mod/mod_alias.html#redirectmatch
It would read:
RedirectMatch 301 /test/[0-9]+ /test
Redirect directive doesn't support Regular Expressions. Instead you can try RewriteRule:
RewriteRule ^test/[0-9]+$ /test [R=301,L]
Looks like this expression will work for you.
/(?:\d*)?\d+/g
http://regexr.com/3e8bk
I would like to redirect all of my blog pages to a new URL.
For example:
I would like to redirect:
http://adaras.se/blogg/page/35
... to:
http://adaras.se/blog/page/35
I am using Redirects Yoast SEO and don't get how to use regex redirects. How can I do this?
You can try:
RewriteCond %{REQUEST_URI} blogg/.*
RewriteRule blogg/(.*)?$ blog/$1 [L]
[EDIT] to add link to online tester.
Yuu can see this rule working for your example here:
and try it yourself on the online .htaccess tester at this URL: http://htaccess.mwl.be/
If it does not work on your Yoast plugin, check the generated .htaccess file and you can also try to match everything to redirect to Google for instance to see if your htaccess is really taken in consideration! :)