Apache rewrite url help, add string in the middle of the url - apache

I need some help with my apache rewrites since I am quite bad at it.
I want to make an apache rewrite rule where I can rewrite links from my old site format to the new one.
http://foo.bar/old/$1 to http://foo.bar/new/$1. This should also work when old is a random string or empty.
Thanks in advance.

If anyone has the same issue as i did, i found the solution:
RedirectMatch 301 ^/([-0-9a-zA-Z])/(.)$ http://foo.bar/new/$2

Related

Htaccess Redirect with Regular Expressions

I have a question regarding htaccess configuration win a PHP Project
I have this code to use redirection
DirectoryIndex src/client/index.php
Redirect /app/about http://localhost/app/?p=about
Redirect /app/contact http://localhost/app/?p=contact
Inside src/client/index.php I check the GET[p] to include the page requested.
It works... but I dont want to modify the htaccess file each time I add a new page.
I would like to implement something like this but it doesnt work, Im pretty sure I have syntax errors
Redirect ^/app/(.*) http://localhost/app/?p=$1
Thanks for your time :)
You just need to use RedirectMatch:
RedirectMatch ^/app/(.*) http://localhost/app/?p=$1

Remove /amp from URL with .htaccess

I need to use .htaccess to remove /amp from a certain URL structure. I have incoming links that look like this:
https://domain/store/amp and https://domain/store/product/amp
In both cases, the /amp on the end results in 404 errors. I just need to get rid of it, because fixing the problem at the source is not currently an option. I intend to put the .htaccess in the /store/ directory path.
I have looked around for similar examples and tried to modify them, but rewrites make my head spin in short order. Thanks for the help.
You can use this Redirect
RedirectMatch 301 ^/(.+)amp$ /$1

Apache Rewrite keeping the original QueryString on the Rewrite URL

I have a problem.
The follow code is my Apache configuration:
RewriteRule ^/page-(.*)$ https://%{SERVER_NAME}:%{SERVER_PORT}/site/spaces/portal/page/$1 [QSA]
So, its OK. But, when I access my page like:
www.mysite.com/page-page?query=blah
It redirects the URL without the query=blah params.
What I'm doing wrong?
Thanks
I am not sure why they are missing, but you should be able to put them back with %{QUER­Y_S­TRING}.

RedirectMatch query string issue

I am moving content on a previous website where 2 copies of the site were made to translate it so that i only have one remaining.
To provide fallback access, i wrote this rewritematch rule :
RedirectMatch 301 /(en|fr)/(.+)\.php\??(.+)? /$2.php?locale=$1&$3
But it doesn't seem to be working.
If i do the following instead, everything works fine but i lose the query string passed in the original link which i really want.
RedirectMatch 301 /(en|fr)/(.+)\.php /$2.php?locale=$1&$3
Is there something i am doing wrong? Or are query strings not supported? (I checked but there seems to be no limitations on the apache docs).
Thanks in advance for your help!
try
RedirectMatch 301 /(en|fr)/(.+)(\.php\?)?(.+)?

htaccess rewrite rules are not working with urls that end with .cfm

I'm working on fixing all my URL's to be shorter with 301 redirects. I have fix almost all of them, however there is a url that is ending with .cfm that will not rewrite.
FROM: http://www.mydomain.com/index.cfm/catlink/17/pagelink/7/sublink/34/art/41/rec/1/page.cfm
TO: http://www.mydomain.com/story/resources/health/page/168/page.html
If I change /page.cfm to /page.html then the rewrite will work.
Here is the rewrite rule that works for my other urls
RewriteRule ^index.cfm/catlink/([a-zA-Z0-9/-]+)([/])pagelink/([a-zA-Z0-9/-]+)([/])sublink/([a-zA-Z0-9/-]+)([/])art/([a-zA-Z0-9/-]+)(.*)$
http://localhost/index.cfm?page=moved&cat=$3&subcat=$5&article=$7&story=$8 [R=301]
Why does it work when the URL ends with .html but not when it ends with .cfm? What am I doing wrong?
This is current link and will not work:
http://www.mydomain.com/index.cfm/catlink/17/pagelink/7/sublink/34/art/41/rec/1/page.cfm
If I manually change the end of it to .html, I can get it to work:
http://www.mydomain.com/index.cfm/catlink/17/pagelink/7/sublink/34/art/41/rec/1/page.html
The issue is that Apache httpd is passing it off to Tomcat before Apache looks at the .htaccess. To test this, move your rewrite rules into your vhost. If they work, then that's what the problem was.
First off, change your the first part of your RewriteRule to be the following, more concise expression:
^index.cfm/catlink/(\d+)/pagelink/(\d+)/sublink/(\d+)/art/(\d+)/(.*)$
I believe that alone might resolve the issue. However, if it does not, and you don't care about the rest of the URL, try the following:
^index.cfm/catlink/(\d+)/pagelink/(\d+)/sublink/(\d+)/art/(\d+)/
Note: this removes the anchor ($) and therefore allows the URL to be open ended.